无法在局部变量上找出NameError

Kri*_*hna 0 ruby ruby-on-rails view-helpers ruby-on-rails-4

我的application_helper.rb文件包含以下代码

module ApplicationHelper
def current_user
      @current_user ||= User.find(session[:user_id]) if session[:user_id]
    end
end
Run Code Online (Sandbox Code Playgroud)

我有一个有一个方法的帖子控制器

def create
@post = Post.new(params[:post])
@post.posted_by_uid=current_user.id
@post.posted_by=current_user.first_name+" "+current_user.last_name
@post.ups=0
@post.downs=0
@post.save
respond_to do |format|
    if @post.save
        format.html {redirect_to root_path}
        format.json {render :json => @post, :status => :created, :location => @post}
    else
        format.html {redirect_to root_path}
        format.json {render :json => @post.errors, :status => :unprocessable_entity }
    end
end
end
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试通过以下视图提交表单时

<%= form_for(@post) do |f| %>
<div class="fields">
<%= f.label "Post" %><br />
<%= f.text_field :post %>
</div>
<div class="actions" id="refreshposts">
<%= f.submit("Post") %>
</div>
<% end %>
Run Code Online (Sandbox Code Playgroud)

它返回一个错误未定义的局部变量或方法`current_user'

任何帮助表示赞赏

Kir*_*rat 7

ApplicationHelperApplicationController类中包含模块,以便current_user控制器中可以使用该方法.

class ApplicationController < ActionController::Base
  include ApplicationHelper ## Add this line
  ## ...
end
Run Code Online (Sandbox Code Playgroud)