【问题标题】:[undefined local variable or method `current_user' in controller][undefined local variable or method `current_user' in controller]
【发布时间】:2019-11-30 08:17:21
【问题描述】:

我已经解决了这个问题 1 小时,但不知道为什么它不起作用。

我不使用 gem 设计。 我有用户模型、帖子模型、UsersController.rb、PostsController.rb 和 1 个帮助器,如下所示

  1. PostsController.rb:
class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]

  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.all
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
  end

  # GET /posts/new
  def new
    @post = Post.new
  end

  # GET /posts/1/edit
  def edit
  end

  # POST /posts
  # POST /posts.json
  def create
    @post = current_user.posts.build(post_params)

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render :show, status: :created, location: @post }
      else
        format.html { render :new }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /posts/1
  # PATCH/PUT /posts/1.json
  def update
    respond_to do |format|
      if @post.update(post_params)
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { render :show, status: :ok, location: @post }
      else
        format.html { render :edit }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @post.destroy
    respond_to do |format|
      format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_post
      @post = Post.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def post_params
      params.require(:post).permit(:caption, :user_id)
    end
end

  1. ApplicationHelper.rb
module ApplicationHelper
    def current_user
        session[:user_id] && User.find(session[:user_id])
    end
end

current_user 辅助方法适用于所有视图。

据我了解,PostsController 继承自 ApplicationController,因此它从 ApplicationHelper 中获取所有帮助程序。我仍然不明白为什么这不起作用。

感谢您的帮助。

【问题讨论】:

标签: ruby-on-rails ruby controller helper


【解决方案1】:

尝试将此添加到ApplicationController.rb

helper_method :current_user

【讨论】:

  • 您还应该将当前用户方法直接移动到控制器、关注点或应用程序控制器中。
【解决方案2】:

在 Rails 5 中调用辅助方法:

# sample : 

module UsersHelper
  def full_name(user)
    user.first_name + user.last_name
  end
end

class UsersController < ApplicationController

  def update
    @user = User.find params[:id]
    if @user.update_attributes(user_params)
      notice = "#{helpers.full_name(@user) is successfully updated}"
      redirect_to user_path(@user), notice: notice
    else
      render :edit
    end
  end
end

在 Rails 5 之前:


# sample :

module UsersHelper
  def full_name(user)
    user.first_name + user.last_name
  end
end

class UsersController < ApplicationController
  include UsersHelper

  def update
    @user = User.find params[:id]
    if @user.update_attributes(user_params)
      redirect_to user_path(@user), notice: "#{full_name(@user) is successfully updated}"
    else
      render :edit
    end
  end
end

【讨论】:

    猜你喜欢
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 2018-08-03
    • 1970-01-01
    相关资源
    最近更新 更多