访问before_action中的application_helper

时间:2014-04-03 21:00:52

标签: ruby-on-rails ruby ruby-on-rails-4 helper

我的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中访问帮助程序?

1 个答案:

答案 0 :(得分:1)

ApplicationHelper中加入ProjectsController

class ProjectsController < ApplicationController
    include ApplicationHelper 
    # ... 
end

助手不能直接在控制器内访问,而不是可以自由访问的视图层。