注册后设计重定向

设计给了我一个相当困难的时间。 现在,除了注册重定向之外,其他一切似乎都在工作。 我希望设计在注册或登录时重定向到我的城镇控制器(登录实际上有效)。

我试过覆盖RegistrationsController,我尝试添加一个applicationController函数,如:

def after_sign_in_path_for(resource_or_scope) if resource_or_scope.is_a?(User) town_path else super end end 

不过,我得到了同样的错误:

 NoMethodError in User/townController#index You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.* 

说真的,我找不到办法去做。 有什么想法吗? 🙂

编辑:我的路线

  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 GET /users/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"} 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"} 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 /(.:format) {:action=>"index", :controller=>"home"} user_root /user(.:format) {:action=>"index", :controller=>"user/town"} home /home(.:format) {:action=>"index", :controller=>"home"} town /town(.:format) {:action=>"index", :controller=>"town"} inbox /messages(.:format) {:action=>"index", :controller=>"messages"} inbox /messages/inbox(.:format) {:action=>"inbox", :controller=>"messages"} 

Routes.rb:

  devise_for :users root :to => "home#index" namespace :user do root :to => "town#index" end scope :path => '/home', :controller => :home do match '/' => :index, :as => 'home' end scope :path => '/town', :controller => :town do match '/' => :index, :as => 'town' end ...... 

这就是我的工作方式。

 # In your routes.rb match 'dashboard' => 'user_dashboard#index', :as => 'user_root' 

然后确保你没有before_filter :authenticate_user! 在你的home#index控制器上设置。

我使用Devise 1.3.4与Ruby On Rails 3.0.7。 在检查了互联网的解决方案后,我所做的只是粘贴以下代码

* 首先) *要在成功注册到欢迎页面后重定向,请在/config/routes.rb中放置以下内容(注意,将:user用您提供给devise_for的参数替换为devise_for

 namespace :user do root :to => "welcome#index" end 

* second) *要在注销后重定向(也到欢迎页面),请将此方法放在/app/controllers/application_controller.rb中:

 private # Overwriting the sign_out redirect path method def after_sign_out_path_for(resource_or_scope) welcome_index_path end 

这对我有用,我希望它适用于所有人。

这个问题的答案对我来说非常有效,在Devise Wiki上有解释:
如何:成功注册时重定向到特定页面(注册)

由于没有人回答,我会插话。你确定这是造成错误的代码吗? 你的resource_or_scope是nil使得看起来像Devise是问题,但我怀疑是这样的。 所以我认为问题出在其他地方。

我会尝试以下方法,以确保它首先工作。

  def after_sign_in_path_for(resource) "http://www.google.com" end 

如果确实有效,那么尝试检查resource变量以确保它不是零。

你有没有尝试覆盖sign_in_and_redirect方法; 它会对你有用

 def sign_in_and_redirect(resource_or_scope,resource) if resource_or_scope == :user redirect_to town_path else super end end 

托尼说的是什么。 命名空间不应该以这种方式使用。 我不知道是谁提出这个根本不起作用的黑客,但不幸的是,这是谷歌搜索时出现的第一个解决方案之一。

 namespace :user do root :to => "town#index" end 

将’/ user’设置为user_root_path并指向您没有的’user / town’控制器的索引操作。 你所拥有的是“城镇”控制者。 这就是你得到错误的原因。 你需要的是什么

 get '/town', to: 'town#index', as: 'user_root' 

这样,’/ town’指向’town #index’并设置为你的user_root_path,这是用户在用户之后重定向到的

  1. 登入
  2. 注册
  3. 更新密码

所以你甚至不需要覆盖after_sign_in_path_for ,这与许多人认为的相反,不是解决设计重定向问题的最佳位置。

在Rails 4.1+和Devise 3.2上

不确定这是否有用但我遇到了上述相同的问题,但不同的是我正在使用自定义注册控制器。

从RegistrationsController的设计源代码看来,它正在调用sign_in_and_redirect,后者又将其传递给redirect_location方法。

我重写redirect_location只是从after_sign_up_path_for(资源)而不是stored_location_for(范围)||返回路径。 after_sign_up_path_for(资源)。

在after_sign_up_path_for(资源)中,我根据用户类型自定义重定向到正确路径的方法

适用于Rails 3.0.5和Devise 1.3.0。

问题是,当签入刚刚创建的资源时,Devise会绕过after_sign_in_path_for(resource) 。 你需要的是after_sign_up_path_for(resource)

 # override in your ApplicationController or custom Devise::RegistrationsController def after_sign_up_path_for(resource) if resource.is_a?(User) town_path else super end end 

您仍然需要使用after_sign_in_path_for(resource)覆盖来正确地将后续登录重定向到资源。

我有类似的问题。 最终在应用程序控制器中使用if语句。 如果用户已登录且他们没有配置文件,则路由到new_profile_path