gpt4 book ai didi

ruby-on-rails - 如何设计 RegistrationsController 以仅在用户已登录时显示注册页面?

转载 作者:数据小太阳 更新时间:2023-10-29 08:16:02 24 4
gpt4 key购买 nike

我试图通过谷歌和此处找到解决方案,但找不到...
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。如果我还没有登录,我希望 Controller 将我重定向到 sign_in 页面。当然我可以在 new 方法中检查它并重定向,但这不是一个好的解决方案......我确定是一种更优雅的方式。我什至尝试使用 prepend_before_filter :authenticate_user! 但它也不起作用。

编辑

我已经在 routs.rb 中为这个 Controller 定义了路由

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

最佳答案

Devise::RegistrationsControllerrequire_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

关于ruby-on-rails - 如何设计 RegistrationsController 以仅在用户已登录时显示注册页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9799593/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com