0

In my edit profile page

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>

In my routes

  devise_for :users, 
    :controllers => { 
      :registrations => "registrations", 
      :sessions => "sessions", 
      :invitations => 'invitations' 
    },
    :path => '/',
    :path_names => {
        :sign_in  => 'login',
        :sign_out => 'logout',
        :sign_up => 'signup'
    }

  devise_scope :user do
    root :to => "registrations#new"
    get '/me/profile' => 'registrations#edit', :as => "myprofile"
    get "/me/invites" => 'invitations#new', :as => 'myinvites'
  end

rake routes:

        new_user_session GET        /login(.:format)                                    {:action=>"new", :controller=>"sessions"}
            user_session POST       /login(.:format)                                    {:action=>"create", :controller=>"sessions"}
    destroy_user_session DELETE     /logout(.:format)                                   {:action=>"destroy", :controller=>"sessions"}
cancel_user_registration GET        /cancel(.:format)                                   {:action=>"cancel", :controller=>"registrations"}
       user_registration POST       /                                                   {:action=>"create", :controller=>"registrations"}
   new_user_registration GET        /signup(.:format)                                   {:action=>"new", :controller=>"registrations"}
  edit_user_registration GET        /edit(.:format)                                     {:action=>"edit", :controller=>"registrations"}
                         PUT        /                                                   {:action=>"update", :controller=>"registrations"}
                         DELETE     /                                                   {:action=>"destroy", :controller=>"registrations"}
                    root            /                                                   {:controller=>"registrations", :action=>"new"}
               myprofile GET        /me/profile(.:format)                               {:controller=>"registrations", :action=>"edit"}

Now my edit user profile page does not work anymore, when I include :path => '/'. Clicking Update Profile does not update my user profile.

The edit user profile form is now action = '/' (does not work) instead of previously action = '/users' (works)

Min Ming Lo
  • 2,398
  • 2
  • 18
  • 25

2 Answers2

0

Setting up custom Devise routes is described partially on the Devise Github Wiki.

However removing the mapping (in this case 'user') is not completely clear. The described :path => '' also breaks stuff.

So if you want: /sign_in instead of /users/sign_in, /register instead of /users/register, etc..

What you can do:

Create custom controllers for Sessions and Registrations. In these controllers set:

class Users::RegistrationsController < Devise::RegistrationsController

  prepend_before_filter :set_devise_mapping

private

  def set_devise_mapping
    request.env["devise.mapping"] ||= Devise.mappings[:user]
  end

end

(same for Users::SessionsController < Devise::SessionsController)

Next add the following to your routes.rb:

  devise_for :users,
    :skip => [:sessions, :registrations],
    :controllers => {
      :omniauth_callbacks => 'users/omniauth_callbacks',
      :registrations => 'users/registrations',
      :passwords => 'users/passwords',
      :sessions => 'users/sessions'
    },
  # Setting empty path gives errors in registration_path.
  # So we overwrite them here.
  get 'sign_in' => 'users/sessions#new', :as => :new_user_session
  post 'sign_in' => 'users/sessions#create', :as => :user_session
  delete 'sign_out' => 'users/sessions#destroy', :as => :destroy_user_session
  get 'register' => 'users/registrations#new', :as => :new_user_registration
  post 'register' => 'users/registrations#create', :as => :user_registration

To also allow account editing you should add your own edit and update routes:

  namespace :users do
    resource :registration, only: [:edit, :update], path: ''
  end
joost
  • 6,549
  • 2
  • 31
  • 36
0

I believe that

...,
:controllers => { 
      :registrations => "registrations", 
      :sessions => "sessions", 
      :invitations => 'invitations' 
    },
    :path => '/',
    :path_names => {
        :sign_in  => 'login',
        :sign_out => 'logout',
        :sign_up => 'signup'
    }

Is not needed. You only need devise_for :users as stated in the README.rdoc

Hope this helps.

Mab879
  • 608
  • 16
  • 33
  • They aren't needed to function but are needed for customizations. – Dex Dec 06 '11 at 03:25
  • As stated http://stackoverflow.com/questions/3827011/devise-custom-routes-and-login-pages, I added those because I do not want "users/sign_in" etc. to be functional. I only want `/login` to be the only place to sign in. – Min Ming Lo Dec 06 '11 at 04:16