设计自定义路由和登录页面 [英] Devise Custom Routes and Login Pages
问题描述
我试图让我的Rails应用程序(Ruby 1.9.2和Rails 3)中的自定义路由工作。
I'm trying to get Custom Routes working in my Rails application (Ruby 1.9.2 with Rails 3).
这是我的config / routes.rb文件
This is my config/routes.rb file
match '/dashboard' => 'home#dashboard', :as => 'user_root'
devise_for :user do
get "/login", :to => "devise/sessions#new" # Add a custom sign in route for user sign in
get "/logout", :to => "devise/sessions#destroy" # Add a custom sing out route for user sign out
get "/register", :to => "devise/registrations#new" # Add a Custom Route for Registrations
end
但提交/ login或/ register上的表单发送给用户/ sign_in
和users / sign_up。如何防止这种情况发生。或者甚至更好地确保默认情况下,所有对用户/ sign_in等的请求都会转到相关路由,而不是由Devise生成的默认路由。
But submitting the form on /login or /register goes to users/sign_in and users/sign_up. How do I prevent this from happening. Or even better make sure that by default all requests for users/sign_in etc go to the relevant routes and not the default routes generated by Devise.
还如何使登录表单部分将其包含在任何控制器中?所以我可以在首页(home#index)上登录页面,而不是用户/ sign_in?
Also how can I make the login form a partial to include it in any controller? So that I can have the Login Page on the homepage (home#index) and not on users/sign_in?
我正在使用Devise 1.1.3和Rails 3 Ruby 1.9.2,Mac OSX Snow Leopard。
I'm using Devise 1.1.3 with Rails 3 on Ruby 1.9.2, on Mac OSX Snow Leopard.
谢谢!
推荐答案
对于Devise 1.1.3,以下内容应该是
With Devise 1.1.3 the following should work
devise_for :user, :path => '', :path_names => { :sign_in => "login", :sign_out => "logout", :sign_up => "register" }
它创建的路由不会附加/ user / ...因为:path
参数是一个空字符串。 :pathnames
哈希将根据您喜欢的路由命名。 Devise将内部使用这些路由,因此提交到/ login将按照您的意愿工作,而不会将您带到/ user / log_in
The routes it creates will not be appended with "/user/..." because of the :path
parameter being an empty string. The :pathnames
hash will take care of naming the routes as you like. Devise will use these routes internally so submitting to /login will work as you wish and not take you to /user/log_in
要将登录表单添加到您的首页, Devise Wiki的信息:
http://github.com/plataformatec/devise/wiki/How-To:-Display-a-custom-sign_in-form-anywhere-in-your-app
To add a login form to your front page there's info at the Devise Wiki: http://github.com/plataformatec/devise/wiki/How-To:-Display-a-custom-sign_in-form-anywhere-in-your-app
或者做这样的事情:
<%= form_tag new_user_session_path do %>
<%= text_field_tag 'user[email]' %>
<%= password_field_tag 'user[password]' %>
<%= submit_tag 'Login' %>
这篇关于设计自定义路由和登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!