views:

174

answers:

4

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

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

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.

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?

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

Thanks!

+1  A: 

Use this at the top of your routes.rb file

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

Jayaram
Will this work for having the login pages on the home page. They reside in the home#index. And devise (http://github.com/plataformatec/devise/) uses the user model.
Karthik Kastury
Doesn't work. My controller's name is home and it doesn't detect the route / on the home page.
Karthik Kastury
A: 

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

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.

shingara
I don't want the users/sign_in route to work. I only want the custom routes to work, and they should be active in all the controllers and views that use it.
Karthik Kastury
Doesn't work. !
Karthik Kastury
A: 

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.

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

Vlada
Canthiswait's solution works awesome, so I didn't try creating a new custom controller.
Karthik Kastury
gr8 :)of course you don't need controller setting :path_names is crucial
Vlada
+1  A: 

With Devise 1.1.3 the following should work

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

The routes it creates will not be appended with "/user/..." because of the :as 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

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

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' %>
canthiswait
Works great :) Thanks for the help.
Karthik Kastury