我的路线文件:
get "public/index"
get "public/timeline"
devise_for :users
match '/user' => "public#index", :as => :user_root
namespace :user do
root :to => "public#index"
end
devise_scope :user do
get "/login" => "devise/sessions#new"
get "/logout" => "devise/sessions#destroy"
end
root :to => 'public#index'
应用程序 Controller
Application Controller:
class ApplicationController < ActionController::Base
protect_from_forgery
def after_sign_in_path_for(resource)
stored_location_for(resource) || root_url
end
end
我想设置重定向到根 URL
我查看了https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in它似乎不起作用......也许我的路线是错误的?
编辑: 问题出在登录操作上。 这是提交登录名/密码后的控制台输出:
Started POST "/users/sign_in" for 127.0.0.1 at 2011-10-25 13:31:16 -0400
Processing by Devise::SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"NeWp8HyAg5p9kBOG9ff7U/Z34IGgUorEWNk9wGFn8T0=", "user"=>{"login"=>"DerNalia", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"}
Completed in 20ms
Processing by Devise::SessionsController#new as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"NeWp8HyAg5p9kBOG9ff7U/Z34IGgUorEWNk9wGFn8T0=", "user"=>{"login"=>"DerNalia", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"}
Rendered devise/shared/_links.erb (2.3ms)
Rendered devise/sessions/new.html.erb within layouts/application (6.1ms)
Rendered devise/menu/_registration_items.html.erb (0.7ms)
Rendered devise/menu/_login_items.html.erb (0.5ms)
Completed 200 OK in 199ms (Views: 20.2ms | ActiveRecord: 0.8ms)
最佳答案
Devise 有一个登录后的重定向 Hook ,您可以在应用程序 Controller 中覆盖该 Hook 来完成此操作。首先,您需要将设计路径设置为 nil:
def stored_location_for(resource)
nil
end
然后您可以像这样分配重定向:
def after_sign_in_path_for(resource)
#path_to_redirect_to
end
这就是用户登录后更改重定向的全部内容。
编辑
我更深入地查看了您的代码,发现了您所看到的登录问题。在新的 session 页面中,登录文本框指向 :username
而不是 :login
,因此请更改
<%= f.text_field :username %></div>
到此
<%= f.text_field :login %></div>
这样就可以解决您的登录问题了。我还注意到,在您的 Devise 初始化程序文件中,您在 :login
和 :email
上设置身份验证 key 。这对于 Devise 来说不是最佳实践,建议您将其更改为严格登录。
关于ruby-on-rails - RoR : Devise: After logging in, 重定向到登录页面,登录后如何设置重定向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7870657/