gpt4 book ai didi

ruby-on-rails - 用户确认帐户后覆盖重定向路径 - Devise

转载 作者:行者123 更新时间:2023-12-03 00:38:20 25 4
gpt4 key购买 nike

我想要发生的是,一旦用户单击确认链接 - 并且他们的帐户已成功确认 - 如果他们分配了特定角色(已成功分配在一个角色中),他们应该被重定向到某个指定的路径User 模型上的 after_create 回调)。

我创建了一个RegistrationsController:

class RegistrationsController < Devise::RegistrationsController
protected

def after_sign_up_path_for(resource)
if resource.has_role? :seller
new_item_path
else
root_path
end
end
end

但是,这总是重定向到root_path....即使我已经验证,用户确实具有该角色。

编辑 1

似乎确认请求从未发送到RegistrationsController:

Started GET "/users/confirmation?confirmation_token=KRwZ7MChtxxq4sxxkDLq" for 127.0.0.1 at 2013-05-07 03:52:56 -0500
Processing by Devise::ConfirmationsController#show as HTML
Parameters: {"confirmation_token"=>"KRwZ7MChtxxq4sxxkDLq"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."confirmation_token" = 'KRwZ7MChtxxq4sxxkDLq' LIMIT 1
(0.1ms) BEGIN
(0.5ms) UPDATE "users" SET "confirmation_token" = NULL, "confirmed_at" = '2013-05-07 08:52:56.846852', "updated_at" = '2013-05-07 08:52:56.847730' WHERE "users"."id" = 9
(0.8ms) COMMIT
(0.1ms) BEGIN
(0.4ms) UPDATE "users" SET "last_sign_in_at" = '2013-05-07 08:52:56.852250', "current_sign_in_at" = '2013-05-07 08:52:56.852250', "last_sign_in_ip" = '127.0.0.1', "current_sign_in_ip" = '127.0.0.1', "sign_in_count" = 1, "updated_at" = '2013-05-07 08:52:56.853180' WHERE "users"."id" = 9
(0.7ms) COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 31ms (ActiveRecord: 3.2

编辑2

这就是我的 routes.rb 的样子

  devise_for :users, :path_names => { :sign_up => "register", 
:sign_in => "login",
:sign_out => "logout",
:settings => "settings" },
:controllers => { :registrations => "registrations" }

devise_scope :user do
get "login", :to => "devise/sessions#new"
get "register", :to => "registrations#new"
get "settings", :to => "devise/registrations#edit"
get "logout", :to => "devise/sessions#destroy"
end

最佳答案

我终于明白了。

我必须创建一个覆盖 Devise 的确认 Controller 的 ConfirmationsController.rb 类。

class ConfirmationsController < Devise::ConfirmationsController
protected
def after_confirmation_path_for(resource_name, resource)
if resource.has_role? :seller
new_item_path
else
root_path
end
end
end

然后在我的routes.rb中:

  devise_for :users, :path_names => { :sign_up => "register", 
:sign_in => "login",
:sign_out => "logout",
:settings => "settings" },
:controllers => {:confirmations => "confirmations"}

devise_scope :user do
get "login", :to => "devise/sessions#new"
get "register", :to => "devise/registrations#new"
get "settings", :to => "devise/registrations#edit"
get "logout", :to => "devise/sessions#destroy"
end

这对我来说似乎很有效。

关于ruby-on-rails - 用户确认帐户后覆盖重定向路径 - Devise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16415110/

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