I just recently started programming in Ruby on Rails, and I was wondering if some of you could look over my routes.rb file that I am using so far and tell me if I am over thinking this.
I am aware of the whole RESTful approach in RoR and I am trying to stick to it, but I am not sure if I am on track. So far my application only has the following functionality:
- User registration
- User activation (via email link)
- User can request activation to be resent
- User log in
- User log out
- User requests password reset (gets an email)
- Basic UCP (change email and password)
I am using a lot of redirect_to *_url and *_path, so I want a lot of named routes. I am trying to explicitly declare only routes that are allowed. Thanks for your input.
MyApp::Application.routes.draw do
get 'home' => 'pages#index', :as => 'home'
get 'testing' => 'pages#testing', :as => 'testing'
get 'register' => 'users#new', :as => 'register'
post 'users/create'
resources :users, :only => [
:new,
:create
]
get 'activation' => 'activations#new', :as => 'activation'
get 'activate/:token' => 'activations#activate', :as => 'activate'
post 'activations/edit'
resources :activations, :only => [
:new,
:activate,
:edit
]
get 'login' => 'sessions#new', :as => 'login'
get 'logout' => 'sessions#destroy', :as => 'logout'
get 'sessions/destroy'
resources :sessions, :only => [
:new,
:create,
:destroy
]
get 'forgot_password' => 'resets#new', :as => 'forgot_password'
post 'resets/create'
get 'activate_password/:token' => 'resets#activate', :as => 'activate_password'
put 'save_password' => 'resets#save', :as => 'save_password'
resources :resets, :only => [
:new,
:create,
:activate,
:save
]
get 'ucp' => 'ucp#show', :as => 'ucp'
post 'ucp_update' => 'ucp#update', :as => 'ucp_update'
resources :ucp, :only => [
:show,
:update
]
root :to => 'pages#index'
end
When you use
resources, it automatically makes named routes for you. I won’t go through your entire routes file, but one example:Could be:
which will produce new_activation_path, edit_activation_path, and activate_activation_path
Go to the Rails Routing Guide for a lot of cool stuff you can do in routes. For example, if you want to use “register” instead of “new” for your Users paths: