如何:设计after_sign_up_redirect? [英] How-to: Devise after_sign_up_redirect?

查看:95
本文介绍了如何:设计after_sign_up_redirect?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图按照这里的说明( GitHub Devise Wiki ),但它不适用于我正在试图让Devise在用户注册后重新定向/欢迎。



我创建了一个注册控制器,但它不会进入视图。我在这里做错什么?



Ruby 1.9.2
Rails 3.1.0.rc4
Devise 1.4.2



谢谢



更新:



由于某些原因, after_sign_up_path_for 未触发,但触发了 after_sign_in_path_for 。我仍然希望通过



_app / controllers / registrations_controller.rb _


$ b $获得 after_sign_up_path_for b

  class RegistrationsController< Devise :: RegistrationsController 
protected

def after_sign_up_path_for(resource)
welcome_path
end
end
pre>

这是我的 config / 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 => registration} do
get/ login,:to => devise / sessions#new
get/ register,:to => devise / registrations#new
get/ logout,:to => devise / sessions#destroy
get'/ account'=> '设计/注册#编辑'
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 =>创建,: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 =>摧毁,:控制器= >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}
注册GET /register(.:format){:controller =>devise / registrations,:action =>new}
logout GET /logout(.:format){:controller =>devise / sessions,:action =>destroy}
account GET /account(.:format){:co ntroller =>devise / registrations,:action =>edit}
new_user_session GET /users/sign_in(.:format){:action =>new,:controller =>设计/会话}
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 / password}
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 =>registrationrations}
GET /users/sign_up(.:format){:action =>new,:controller =>registrations}
GET /users/edit(.:format){:action => ;edit,:controller =>registrations}
PUT /users(.:format){:action =>update,:controller =>registrationrations}
DELETE /users(.:format){:action =>destroy,:controller =>registrationrations}


解决方案

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

  MyApp :: Application.routes.draw do 
devise_for:users, controllers => {:registrations => registration} 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 ,也如devise wiki所述。你可以告诉它在注册后被重定向到哪里?



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



所以删除它,如上所述更改routes.rb,看看会发生什么。


I tried to follow the instructions here (GitHub Devise Wiki) but it's not working for me.

I'm trying to get Devise to redirect to /welcome after user signs up. I created a Registrations controller but it's never going into the view. What am I doing wrong here?

Ruby 1.9.2 Rails 3.1.0.rc4 Devise 1.4.2

Thank you

UPDATE:

For some reason after_sign_up_path_for is not triggered but after_sign_in_path_for is triggered. I would still like to get after_sign_up_path_for to work though

_app/controllers/registrations_controller.rb_

class RegistrationsController < Devise::RegistrationsController
  protected

  def after_sign_up_path_for(resource)
    welcome_path
  end
end

Here's my config/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

Output from rake routes

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"}

解决方案

You have devise_for :users twice in your routes.rb. Change the routes.rb as follows:

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

If you have activation/deactivation logic, you have to override after_inactive_sign_up_path_for too as mentioned in the devise wiki. Can you tell where it is getting redirected to after sign up?

The reason why after_sign_in_path_for is working may be that you have the same sessions_controller and different registrations_controller, so sign-in works as you expected and the registrations route is getting blocked because you have defined it twice, so all the registration requests and actions are executed in the devise/registrations rather than your custom registrations controller.

So remove that, change the routes.rb as mentioned above, and see what happens.

这篇关于如何:设计after_sign_up_redirect?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆