【问题标题】:Issue with helper_methods from views rails视图导轨中的 helper_methods 问题
【发布时间】:2013-06-27 12:45:55
【问题描述】:

我有一个非常令人沮丧的问题。

我无法从我在 rails 的视图中调用任何辅助方法。这就是我所拥有的:

ApplicationController.rb

class ApplicationController < ActionController::Base

   protect_from_forgery

   def current_user
     @current_user ||= User.find(session[:user_id]) if session[:user_id]
   end

   helper_method :current_user
   helper_method :all

  end

/app/views/welcome/_navi_bar.haml:

%ul.nav.nav-pills.pull-right
   %li.pull-right
      - if current_user
         %a.btn-small{"data-toggle" => "modal", :href => log_out_path, :role => "button"}
           %i.icon-user.icon-white
           %strong
             Log Out
       - else
         %a.btn-small{"data-toggle" => "modal", :href => "#LoginBox", :role => "button"}
           %i.icon-user.icon-white
            %strong
             Login

这是我得到的错误:

undefined local variable or method `current_user' for #<#<Class:0x007ff0a0544668>:0x007ff0a05414b8>

我真的不明白问题是什么。请帮忙!

【问题讨论】:

  • 虽然我不认为它会导致问题,但我建议删除不必要的helper_method :all 行。
  • @Domon 没有帮助:(

标签: ruby-on-rails-3 views helpermethods


【解决方案1】:

您已在应用程序控制器而不是应用程序助手中编写代码 这就是你的方法没有被调用的原因

如果你想检查当前用户是否登录 您可以在应用程序控制器中使用过滤器之前并调用该方法

当你不需要检查方法时,在那个地方的过滤器之前添加跳过

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-06
    相关资源
    最近更新 更多