11

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
CodeDaily
  • 707
  • 4
  • 16

2 Answers2

18

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
TJ Biddle
  • 4,473
  • 4
  • 33
  • 45
2

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
Andy Gauge
  • 1,313
  • 1
  • 11
  • 24