RegistrationsController

RegistrationsController

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

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。如果我尚未登录,我希望控制器将我重定向到登录页。当然,我可以在new方法中检查它并重定向,但这不是一个好的解决方案…我相信有一种更优雅的方式。我甚至试过使用prepend_before_filter :authenticate_user!,但它也不起作用。
编辑
我在routs.rb中为这个控制器定义了路由
devise_for :users, :controllers => { :sessions => "sessions", :registrations => "registrations" }

最佳答案

Devise::RegistrationsController默认为require_no_authentication before filter
所以需要跳过它:

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

  def new
    puts "Method new was called"
    super
  end

end

09-08 06:08