登录后重定向到特定URL

在Devise 1.0中,Rails 2.3的库是否有办法在登录后重定向到特定的URL而不是root_url?

编辑:忘了提它是Devise 1.0

after_sign_in_path之前,您的用户可能会被重定向。 如果用户尝试直接转到受身份validation保护的页面,则会发生这种情况。 如果您的root_path (’/’)受到身份validation保护,则会一直发生这种情况。

有关此主题的谷歌小组讨论:

快速而肮脏的解决方案是覆盖stored_location_for以始终返回nil如下所示:

 class ApplicationController < ActionController::Base ... private def stored_location_for(resource_or_scope) nil end def after_sign_in_path_for(resource_or_scope) my_favorite_path end end 

我认为Devise中的after_sign_in_path_for方法就是你要找的。

在ApplicationController中定义该方法,它将覆盖Devise的默认实现。 这是文档指定要执行的操作。

详情请访问: http : //rdoc.info/github/plataformatec/devise/master/Devise/Controllers/Helpers : after_sign_in_path_for

假设您想在登录后显示用户的仪表板。

 class HomesController < ApplicationController def index if current_user //returns nil if not logged in @users = User.all render :dashboard end end def dashboard @users = User.all end end 

在routes.rb中:

 root :to => 'homes#index' 

如果已登录,则在索引操作中输入if-block并渲染dashboard.erb。 (确保在if-block中初始化dashboard.erb所需的所有变量)否则rails渲染index.erb