undefined method `helper_method' for ApplicationController, Rails 5
Asked Answered
G

2

13

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
Greyso answered 26/8, 2016 at 17:28 Comment(0)
S
21

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
Slavery answered 29/9, 2016 at 3:5 Comment(0)
J
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
Jounce answered 26/8, 2016 at 19:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.