如何设置只有用户已登录时才设计RegistrationsController来显示sign_up页面?

我试图通过谷歌和SO在这里找到解决方案但是找不到……
这是唯一的问题。 它只有一个答案,它已被接受,但对我不起作用……这是我的代码:

class RegistrationsController < Devise::RegistrationsController before_filter :authenticate_user! def new puts "Method new was called" super end end 

当我没有登录localhost:3000/sign_up页面正常显示,并且打印了Method new was called 。 如果我还没有登录,我希望控制器将我重定向到sign_in页面。当然我可以用new方法检查它并重定向,但这不是一个好的解决方案……我确信有更优雅的方式。 我甚至尝试使用prepend_before_filter :authenticate_user! 但它也不起作用。

编辑

我在routs.rb中为这个控制器定义了路由

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

Devise::RegistrationsController默认情况下在filter之前有require_no_authentication 。

所以需要跳过它:

 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! 

不再起作用了。

使用authenticate_scope! 代替。