shift66
shift66

Reputation: 11958

How to make devise RegistrationsController to show sign_up page only if user is already signed in?

I tried to find solution by google and here in SO but couldn't found...
This is the only question. It has only one answer and it's accepted but doesn't work for me... Here is my code:

class RegistrationsController < Devise::RegistrationsController

  before_filter :authenticate_user!

  def new
    puts "Method new was called"
    super
  end

end

When I'm not logged in at localhost:3000/sign_up page is displayed normally and Method new was called is printed. I want controller to redirect me into sign_in page if I'm not already signed in. Of course I can check it in new method and redirect but it's not a good solution... I'm sure there is a more elegant way. I even tried to use prepend_before_filter :authenticate_user! but it doesn't work too.

EDIT

I've defined routes for this controller in routs.rb

devise_for :users, :controllers => { :sessions => "sessions", :registrations => "registrations" }

Upvotes: 2

Views: 2189

Answers (2)

Lamp
Lamp

Reputation: 1084

skip_before_filter :require_no_authentication

before_filter :authenticate_user!

does not work any more.

Use authenticate_scope! instead.

Upvotes: 3

lest
lest

Reputation: 8100

Devise::RegistrationsController has require_no_authentication before filter by default.

So it's needed to skip it:

class RegistrationsController < Devise::RegistrationsController
  skip_before_filter :require_no_authentication
  before_filter :authenticate_user!

  def new
    puts "Method new was called"
    super
  end

end

Upvotes: 9

Related Questions