gpt4 book ai didi

ruby-on-rails - 操作方法 : Devise after_sign_up_redirect?

转载 作者:行者123 更新时间:2023-12-04 22:50:35 27 4
gpt4 key购买 nike

我尝试按照此处的说明( GitHub Devise Wiki )进行操作,但对我不起作用。

我试图让 Devise 在用户注册后重定向到/welcome。我创建了一个 注册 Controller ,但它永远不会进入 View 。我在这里做错了什么?

ruby 1.9.2
rails 3.1.0.rc4
设计 1.4.2

谢谢

更新:

出于某种原因 after_sign_up_path_for 未触发但 after_sign_in_path_for 被触发。我还想得到 after_sign_up_path_for 工作虽然

_app/controllers/registrations_controller.rb_

class RegistrationsController < Devise::RegistrationsController
protected

def after_sign_up_path_for(resource)
welcome_path
end
end

这是我的配置/routs.rb
MyApp::Application.routes.draw do
devise_for :users

root :to => 'pages#home'

match "/users", :to => "users#all"
match "/users/:id", :to => "users#show", :as => :user

match "/welcome", :to => "users#welcome", :as => :user

devise_for :users, :controllers => { :registrations => "registrations" } do
get "/login", :to => "devise/sessions#new"
get "/register", :to => "devise/registrations#new"
get "/logout", :to => "devise/sessions#destroy"
get '/account' => 'devise/registrations#edit'
end
end

来自 rake 路由的输出
new_user_session GET    /users/sign_in(.:format)               {:action=>"new", :controller=>"devise/sessions"}
user_session POST /users/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_user_session DELETE /users/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
user_omniauth_callback /users/auth/:action/callback(.:format) {:action=>/(?!)/, :controller=>"devise/omniauth_callbacks"}
user_password POST /users/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
new_user_password GET /users/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
edit_user_password GET /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
PUT /users/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
cancel_user_registration GET /users/cancel(.:format) {:action=>"cancel", :controller=>"devise/registrations"}
user_registration POST /users(.:format) {:action=>"create", :controller=>"devise/registrations"}
new_user_registration GET /users/sign_up(.:format) {:action=>"new", :controller=>"devise/registrations"}
edit_user_registration GET /users/edit(.:format) {:action=>"edit", :controller=>"devise/registrations"}
PUT /users(.:format) {:action=>"update", :controller=>"devise/registrations"}
DELETE /users(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
root / {:controller=>"pages", :action=>"home"}
users /users(.:format) {:controller=>"users", :action=>"all"}
user /users/:id(.:format) {:controller=>"users", :action=>"show"}
user /welcome(.:format) {:controller=>"users", :action=>"welcome"}
login GET /login(.:format) {:controller=>"devise/sessions", :action=>"new"}
register GET /register(.:format) {:controller=>"devise/registrations", :action=>"new"}
logout GET /logout(.:format) {:controller=>"devise/sessions", :action=>"destroy"}
account GET /account(.:format) {:controller=>"devise/registrations", :action=>"edit"}
new_user_session GET /users/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
POST /users/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_user_session DELETE /users/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
user_omniauth_callback /users/auth/:action/callback(.:format) {:action=>/(?!)/, :controller=>"devise/omniauth_callbacks"}
POST /users/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
GET /users/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
GET /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
PUT /users/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
GET /users/cancel(.:format) {:action=>"cancel", :controller=>"registrations"}
POST /users(.:format) {:action=>"create", :controller=>"registrations"}
GET /users/sign_up(.:format) {:action=>"new", :controller=>"registrations"}
GET /users/edit(.:format) {:action=>"edit", :controller=>"registrations"}
PUT /users(.:format) {:action=>"update", :controller=>"registrations"}
DELETE /users(.:format) {:action=>"destroy", :controller=>"registrations"}

最佳答案

您有 devise_for :users在你的 routes.rb 中两次。更改 routes.rb 如下:

MyApp::Application.routes.draw do
devise_for :users, :controllers => { :registrations => "registrations" } do
get "/login", :to => "devise/sessions#new"
get "/register", :to => "devise/registrations#new"
get "/logout", :to => "devise/sessions#destroy"
get '/account' => 'devise/registrations#edit'
end

root :to => 'pages#home'

match "/users", :to => "users#all"
match "/users/:id", :to => "users#show", :as => :user

match "/welcome", :to => "users#welcome", :as => :user

end

如果您有激活/停用逻辑,则必须覆盖 after_inactive_sign_up_path_for也如设计维基中所述。你能知道它在注册后被重定向到哪里吗?

原因 after_sign_in_path_for正在工作的可能是您有相同的 session_controller 和不同的 registrations_controller,因此登录按预期工作,并且由于您定义了两次,注册路由被阻止,因此所有注册请求和操作都在设计/注册中执行而不是您的自定义注册 Controller 。

所以删除它,改变上面提到的 routes.rb,看看会发生什么。

关于ruby-on-rails - 操作方法 : Devise after_sign_up_redirect?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6566103/

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