如何在 users/sign_up 之后 redirect_to root?
How to redirect_to root after users/sign_up with devise?
运行 Rails (v7.0.1), ruby (v3.0.1) 并将设计 gem 添加到 Gemfile。
我正在 localhost:3000/users/sign_up 上的浏览器中测试 users/sign_up 路由。
它成功地将用户保存到数据库(postgresql)
我想在 sign_up 之后 redirect_to root,
就像 sign_in 当前所做的那样,但我得到的是:
Devise::RegistrationsController#create 中没有方法错误
#Devise::RegistrationsController:0x00000000032988 的未定义方法“user_url”
提取的源代码(大约第 231 行):
229
230
231
232
233
234
if options.empty?
recipient.public_send(method, *args)
else
recipient.public_send(method, *args, options)
end
这是堆栈跟踪:
Started POST "/users" for ::1 at 2022-02-03 15:06:14 -0600
Processing by Devise::RegistrationsController#create as TURBO_STREAM
Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"email"=>"test@test.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
TRANSACTION (0.2ms) BEGIN
User Exists? (0.3ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = LIMIT [["email", "test@test.com"], ["LIMIT", 1]]
User Create (0.5ms) INSERT INTO "users" ("email", "encrypted_password", "reset_password_token", "reset_password_sent_at", "remember_created_at", "created_at", "updated_at") VALUES (, , , , , , ) RETURNING "id" [["email", "test@test.com"], ["encrypted_password", "[FILTERED]"], ["reset_password_token", "[FILTERED]"], ["reset_password_sent_at", "[FILTERED]"], ["remember_created_at", nil], ["created_at", "2022-02-03 21:06:14.495644"], ["updated_at", "2022-02-03 21:06:14.495644"]]
TRANSACTION (0.8ms) COMMIT
Redirected to
Completed 500 Internal Server Error in 313ms (ActiveRecord: 1.8ms | Allocations: 7490)
NoMethodError (undefined method `user_url' for #<Devise::RegistrationsController:0x00000000032988>):
我尝试使用以下方法修改应用程序控制器:
ApplicationController.rb
def after_sign_up_path_for(:user)
root_path
end
但运气不好。
也试过rails generate devise:controllers registrations
公开必要的注册#create方法进行修补,但还没有破解
()
您可以使用 after_sign_up_path_for
方法
创建自己的注册控制器作为设计注册控制器的子控制器
# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
private
def after_sign_up_path_for(resource)
root_path # or any other path
end
end
然后将此控制器添加到您通往 devise_for
的路线中,就像这样
# config/routes.rb
devise_for :users, controllers: { registrations: "registrations" }
运行 Rails (v7.0.1), ruby (v3.0.1) 并将设计 gem 添加到 Gemfile。
我正在 localhost:3000/users/sign_up 上的浏览器中测试 users/sign_up 路由。 它成功地将用户保存到数据库(postgresql) 我想在 sign_up 之后 redirect_to root, 就像 sign_in 当前所做的那样,但我得到的是:
Devise::RegistrationsController#create 中没有方法错误 #Devise::RegistrationsController:0x00000000032988 的未定义方法“user_url” 提取的源代码(大约第 231 行): 229 230 231 232 233 234
if options.empty?
recipient.public_send(method, *args)
else
recipient.public_send(method, *args, options)
end
这是堆栈跟踪:
Started POST "/users" for ::1 at 2022-02-03 15:06:14 -0600
Processing by Devise::RegistrationsController#create as TURBO_STREAM
Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"email"=>"test@test.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
TRANSACTION (0.2ms) BEGIN
User Exists? (0.3ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = LIMIT [["email", "test@test.com"], ["LIMIT", 1]]
User Create (0.5ms) INSERT INTO "users" ("email", "encrypted_password", "reset_password_token", "reset_password_sent_at", "remember_created_at", "created_at", "updated_at") VALUES (, , , , , , ) RETURNING "id" [["email", "test@test.com"], ["encrypted_password", "[FILTERED]"], ["reset_password_token", "[FILTERED]"], ["reset_password_sent_at", "[FILTERED]"], ["remember_created_at", nil], ["created_at", "2022-02-03 21:06:14.495644"], ["updated_at", "2022-02-03 21:06:14.495644"]]
TRANSACTION (0.8ms) COMMIT
Redirected to
Completed 500 Internal Server Error in 313ms (ActiveRecord: 1.8ms | Allocations: 7490)
NoMethodError (undefined method `user_url' for #<Devise::RegistrationsController:0x00000000032988>):
我尝试使用以下方法修改应用程序控制器:
ApplicationController.rb
def after_sign_up_path_for(:user)
root_path
end
但运气不好。
也试过rails generate devise:controllers registrations
公开必要的注册#create方法进行修补,但还没有破解
(
您可以使用 after_sign_up_path_for
方法
# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
private
def after_sign_up_path_for(resource)
root_path # or any other path
end
end
然后将此控制器添加到您通往 devise_for
的路线中,就像这样
# config/routes.rb
devise_for :users, controllers: { registrations: "registrations" }