ApplicationController中未定义`helper_method`方法,Rails 5。

13

我正在尝试在我的Rails-API应用程序中使用Doorkeeper集成oAuth2.0。但是我一直得到这个错误:"undefined method `helper_method' for ApplicationController",并且仍然找不到明确的解决方法。下面是我的application_controller.rb类,其中有helper_method。我正在按照以下链接上的教程进行操作,任何帮助将不胜感激。

https://www.sitepoint.com/getting-started-with-doorkeeper-and-oauth-2-0/

class ApplicationController < ActionController::API

private 

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

    helper_method :current_user

end
2个回答

21

尽管Andy Gauge的回答是正确的,但修复方法是错误的。如果您希望在保持应用程序为"rails-api"的同时包含Helpers模块,则只需包含该模块即可。

class ApplicationController < ActionController::API
  include ActionController::Helpers
end

2
因为API没有视图,所以已经移除了helper_method方法。如果你想将current_user方法添加到视图中,请改用ActionController::Base。 Github上的ActionController包含的模块。可以在这里看到模块集合中没有包括AbstractController::Helpers。
在基于Rails 4的文章中,该方法被包含在ActionController::Helpers中。如APIDock中所示。
解决方法:
#application_controller.rb
class ApplicationController < ActionController::Base

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接