我的application_helper.rb文件中有以下内容:
module ApplicationHelper
def require_employer_profile_for_employers(page)
if current_user.type == 'Employer'
if current_user.employer_profile
else
flash[:error] = "You must create a profile before accessing #{page}."
redirect_to new_employer_profile_path
end
end
end
end
我尝试在我的项目控制器中调用它:
before_action "require_employer_profile_for_employers('Projects')"
但我的服务器响应此错误:
NoMethodError (undefined method `require_employer_profile_for_employers' for #<ProjectsController:0x007fb741f82e38>):
如何在项目控制器的before_action中访问帮助程序?
答案 0 :(得分:1)
在ApplicationHelper
中加入ProjectsController
:
class ProjectsController < ApplicationController
include ApplicationHelper
# ...
end
助手不能直接在控制器内访问,而不是可以自由访问的视图层。