Ruby-on-rails 设计自定义路由和登录页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3827011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:17:13  来源:igfitidea点击:

Devise Custom Routes and Login Pages

ruby-on-railsrubyroutingdevisepartials

提问by Karthik Kastury

I'm trying to get Custom Routes working in my Rails application (Ruby 1.9.2 with Rails 3).

我正在尝试让自定义路由在我的 Rails 应用程序中工作(Ruby 1.9.2 with Rails 3)。

This is my config/routes.rb file

这是我的 config/routes.rb 文件

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

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.

但是在 /login 或 /register 上提交表单会转到 users/sign_in 和 users/sign_up。我如何防止这种情况发生。或者甚至更好地确保默认情况下对用户/登录等的所有请求都转到相关路由,而不是由 Devise 生成的默认路由。

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?

另外,如何使登录表单成为部分以将其包含在任何控制器中?这样我就可以在主页(home#index)上而不是在用户/登录页面上拥有登录页面?

I'm using Devise 1.1.3 with Rails 3 on Ruby 1.9.2, on Mac OSX Snow Leopard.

我在 Mac OSX Snow Leopard 上使用 Devise 1.1.3 和 Rails 3 on Ruby 1.9.2。

Thanks!

谢谢!

回答by doritostains

With Devise 1.1.3 the following should work

使用设计 1.1.3 以下应该工作

devise_for :user, :path => '', :path_names => { :sign_in => "login", :sign_out => "logout", :sign_up => "register" }

The routes it creates will not be appended with "/user/..." because of the :pathparameter being an empty string. The :pathnameshash 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

它创建的路由不会附加“/user/...”,因为:path参数是一个空字符串。该:pathnames散列将采取命名的路线,只要你喜欢的保健。Devise 将在内部使用这些路由,因此提交到 /login 将按您的意愿工作,而不会带您到 /user/log_in

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

要将登录表单添加到您的首页,请参阅 Devise Wiki 上的信息:http://github.com/plataformatec/devise/wiki/How-To:-Display-a-custom-sign_in-form-anywhere-in-your -应用程序

Or do something like this:

或者做这样的事情:

 <%= form_tag new_user_session_path do %>
  <%= text_field_tag 'user[email]' %>
  <%= password_field_tag 'user[password]' %>
 <%=  submit_tag 'Login' %>

回答by Waleed Asender

The following worked for me:

以下对我有用:

  devise_for :users do
    get "/login" => "devise/sessions#new"
    get "/register" => "devise/registrations#new"
  end

回答by shingara

You just need don't put your special route in devise_for block

你只需要不要把你的特殊路线放在 devise_for 块中

match '/dashboard' => 'home#dashboard', :as => 'user_root'
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
devise_for :user

Now /login works. /users/sign_in too.

现在 /login 有效。/users/sign_in 也是。

回答by hlcs

Config:

配置:

  devise_scope :user do
    get 'profile/edit'    => 'devise/registrations#edit',   :as => :edit_user_registration
    get 'profile/cancel'  => 'devise/registrations#cancel', :as => :cancel_user_registration
  end

  devise_for  :users,
              :path => '',
              :path_names => {  :sign_in =>       'login',
                                :sign_out =>      'logout',
                                :sign_up =>       '',
                                :registration =>  'register',
                                :edit =>          'edit',
                                :cancel =>        'cancel',
                                :confirmation =>  'verification'  }

Routes:

路线:

  edit_user_registration GET    /profile/edit(.:format)      devise/registrations#edit
cancel_user_registration GET    /profile/cancel(.:format)    devise/registrations#cancel
        new_user_session GET    /login(.:format)             devise/sessions#new
            user_session POST   /login(.:format)             devise/sessions#create
    destroy_user_session DELETE /logout(.:format)            devise/sessions#destroy
           user_password POST   /password(.:format)          devise/passwords#create
       new_user_password GET    /password/new(.:format)      devise/passwords#new
      edit_user_password GET    /password/edit(.:format)     devise/passwords#edit
                         PATCH  /password(.:format)          devise/passwords#update
                         PUT    /password(.:format)          devise/passwords#update
                         GET    /register/cancel(.:format)   registrations#cancel
       user_registration POST   /register(.:format)          registrations#create
   new_user_registration GET    /register(.:format)          registrations#new
                         GET    /register/edit(.:format)     registrations#edit
                         PATCH  /register(.:format)          registrations#update
                         PUT    /register(.:format)          registrations#update
                         DELETE /register(.:format)          registrations#destroy

回答by Vlada

I created my own auth controller and routed devise sessions controller to my controller

我创建了自己的身份验证控制器并将设计会话控制器路由到我的控制器

devise_for :users, 
:controllers => {
    :sessions => 'auth' },

:path => '/',

:path_names => {
    :sign_in  => 'login',
    :sign_out => 'logout' }

This code will add /login and /logout urls.

此代码将添加 /login 和 /logout url。

More about this you can find in source code http://github.com/plataformatec/devise/blob/master/lib/devise/rails/routes.rb

更多关于这个你可以在源代码中找到http://github.com/plataformatec/devise/blob/master/lib/devise/rails/routes.rb

回答by Jayaram

Use this at the top of your routes.rb file

在 routes.rb 文件的顶部使用它

map.connect "users/:action", :controller => 'users', :action => /[a-z]+/i 

use this on where your index file is. if it is on your users model, use the above or change accordingly

在您的索引文件所在的位置使用它。如果它在您的用户模型上,请使用上述内容或进行相应更改