Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined method `helper_method' for ApplicationController, Rails 5

I'm trying to integrate oAuth2.0 In my rails-api only application, using doorkeeper. But I keep getting this error, "undefined method `helper_method' for ApplicationController" and yet could not find a clear solution on how to solve it. bellow is my application_controller.rb class, Which has the helper_method. I'm following tutorial on the link below, Any help will be appreciated.

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
like image 803
CodeDaily Avatar asked Aug 26 '16 17:08

CodeDaily


2 Answers

While Andy Gauge's answer is correct; the fix is incorrect. If you want to include the Helpers module while still keeping your applications as "rails-api" then simply include the module

class ApplicationController < ActionController::API
  include ActionController::Helpers
end
like image 140
TJ Biddle Avatar answered Oct 14 '22 21:10

TJ Biddle


Because APIs don't have a view, the helper_method method has been removed. If you want to add your current_user method to a view, use ActionController::Base instead.

ActionController included Modules on Github. You can see here that AbstractController::Helpers is not included in the Modules collection.

In Rails 4, which the article is based, the method was included in ActionController::Helpers. As seen in the APIDock.

Workaround:

#application_controller.rb
class ApplicationController < ActionController::Base
like image 24
Andy Gauge Avatar answered Oct 14 '22 22:10

Andy Gauge