Prox
Prox

Reputation: 759

undefined method `skip_before_action' when skipping authentication in Devise

I am using this guide for setting up Devise in Rails: http://codepany.com/blog/rails-5-user-accounts-with-3-types-of-roles-devise-rails_admin-cancancan/

It says to place this on your home controller to keep Devise from requesting authentication on your home page:

skip_before_action :authenticate_user!, :only => [:index]

My home controller is called dev so my dev_controller.rb looks like this:

class DevController < ApplicationController
  def index

  skip_before_action :authenticate_user!, :only => [:index]

  end
end

Now when I visit my website, I get this error:

undefined method `before_action' for #<MenuController:0xb193b640>

Any ideas on why I get this error?

Upvotes: 0

Views: 2622

Answers (3)

Sai Ram Reddy
Sai Ram Reddy

Reputation: 1089

If skip_before_action seems to be confusing you can use before_action like below

  before_action :authenticate_user! , except:[index]

Upvotes: 0

Mansi Shah
Mansi Shah

Reputation: 524

As skip_before_action is a Class method, it can't be called from instance method.
It's also known as callback method, here are other methods.

You can update code as,

class DevController < ApplicationController
  skip_before_action :authenticate_user!, :only => [:index]

  def index
  end
end

Upvotes: 2

Rajarshi Das
Rajarshi Das

Reputation: 12340

Please try the below

skip_before_action is should be out side of the index method scope. As before_action or skip_before_action is a class method. Should not call it inside in an instance method(index)

class DevController < ApplicationController
 skip_before_action :authenticate_user!, :only => [:index]

  def index
  end
end

Please refer

Upvotes: 7

Related Questions