简体   繁体   中英

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" }

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
skip_before_filter :require_no_authentication

before_filter :authenticate_user!

does not work any more.

Use authenticate_scope! instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM