Windows 8.1
Ruby 2.0.0
Rails 4.1
在我的lib / assets中,我有一个带有以下内容的gloabl_helper.rb:
def current_user_is_admin
if current_user.try(:role?)
if current_user.role == 'admin'
return TRUE
end
end
return 0
end
在我看来,我有以下内容:
<% if current_user_is_admin == TRUE %>
<% end %>
当我尝试运行该应用时,收到以下错误消息:
undefined local variable or method `current_user_is_admin' for #<#<Class:0x0000000667deb8>:0x00000004fef818>
这是Rails4的变化吗?我理解我应该在帮助程序脚本中放置辅助方法,但是一些帮助程序全局应用,并且应该在lib / assets文件夹中。有什么想法吗?
答案 0 :(得分:0)
TRUE
0
config.autoload_paths += Dir["#{config.root}/lib/**/"
current_user
并非全球可用。它已混合到您的ApplicationController
编写ideomatic ruby代码
def current_admin?
return unless logged_in?
current_user.role == 'admin'
end