Generating Paths and URLs from Code in rails - ruby-on-rails

If I put this in route.rb get '/products/:id', to: 'products#edit' edit action should be called when the url format is /patients/:id does this mean it will be redirected to edit page? I tried this and it is not redirecting.
This is my rake routes output
admin GET /admin(.:format) {:action=>"index", :controller=>"admin"}
GET /products/:id(.:format) {:controller=>"products", :action=>"edit"}
login GET /login(.:format) {:action=>"new", :controller=>"sessions"}
POST /login(.:format) {:action=>"create", :controller=>"sessions"}
logout DELETE /logout(.:format) {:action=>"destroy", :controller=>"sessions"}
users GET (/:locale)/users(.:format) {:action=>"index", :controller=>"users"}
POST (/:locale)/users(.:format) {:action=>"create", :controller=>"users"}
new_user GET (/:locale)/users/new(.:format) {:action=>"new", :controller=>"users"}
edit_user GET (/:locale)/users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
user GET (/:locale)/users/:id(.:format) {:action=>"show", :controller=>"users"}
PUT (/:locale)/users/:id(.:format) {:action=>"update", :controller=>"users"}
DELETE (/:locale)/users/:id(.:format) {:action=>"destroy", :controller=>"users"}
orders GET (/:locale)/orders(.:format) {:action=>"index", :controller=>"orders"}
POST (/:locale)/orders(.:format) {:action=>"create", :controller=>"orders"}
new_order GET (/:locale)/orders/new(.:format) {:action=>"new", :controller=>"orders"}
edit_order GET (/:locale)/orders/:id/edit(.:format) {:action=>"edit", :controller=>"orders"}
order GET (/:locale)/orders/:id(.:format) {:action=>"show", :controller=>"orders"}
PUT (/:locale)/orders/:id(.:format) {:action=>"update", :controller=>"orders"}
DELETE (/:locale)/orders/:id(.:format) {:action=>"destroy", :controller=>"orders"}
line_items GET (/:locale)/line_items(.:format) {:action=>"index", :controller=>"line_items"}
POST (/:locale)/line_items(.:format) {:action=>"create", :controller=>"line_items"}
new_line_item GET (/:locale)/line_items/new(.:format) {:action=>"new", :controller=>"line_items"}
edit_line_item GET (/:locale)/line_items/:id/edit(.:format) {:action=>"edit", :controller=>"line_items"}
line_item GET (/:locale)/line_items/:id(.:format) {:action=>"show", :controller=>"line_items"}
PUT (/:locale)/line_items/:id(.:format) {:action=>"update", :controller=>"line_items"}
DELETE (/:locale)/line_items/:id(.:format) {:action=>"destroy", :controller=>"line_items"}
carts GET (/:locale)/carts(.:format) {:action=>"index", :controller=>"carts"}
POST (/:locale)/carts(.:format) {:action=>"create", :controller=>"carts"}
new_cart GET (/:locale)/carts/new(.:format) {:action=>"new", :controller=>"carts"}
edit_cart GET (/:locale)/carts/:id/edit(.:format) {:action=>"edit", :controller=>"carts"}
cart GET (/:locale)/carts/:id(.:format) {:action=>"show", :controller=>"carts"}
PUT (/:locale)/carts/:id(.:format) {:action=>"update", :controller=>"carts"}
DELETE (/:locale)/carts/:id(.:format) {:action=>"destroy", :controller=>"carts"}
who_bought_product GET (/:locale)/products/:id/who_bought(.:format) {:action=>"who_bought", :controller=>"products"}
products GET (/:locale)/products(.:format) {:action=>"index", :controller=>"products"}
POST (/:locale)/products(.:format) {:action=>"create", :controller=>"products"}
new_product GET (/:locale)/products/new(.:format) {:action=>"new", :controller=>"products"}
edit_product GET (/:locale)/products/:id/edit(.:format) {:action=>"edit", :controller=>"products"}
product GET (/:locale)/products/:id(.:format) {:action=>"show", :controller=>"products"}
PUT (/:locale)/products/:id(.:format) {:action=>"update", :controller=>"products"}
DELETE (/:locale)/products/:id(.:format) {:action=>"destroy", :controller=>"products"}
store /(:locale)(.:format) {:controller=>"store", :action=>"index"}

The route you've got there doesn't do a redirect, it maps the url /patients/:id to the edit action of the PatientsController.
When a user hits the /patients/id url, the edit action will be invoked. By default this tries to render a view with the same name as the action.
If you want to render an edit page, create a view in the /app/views/patients folder called edit.html.erb.
(By the way, the GET /model/:id route is generally used for the show action.)

Related

How do I redirect back to a page I'm currently on?

In my users photo album page they see photos they have uploaded and each photo has a 'make default' link on it.
When the user clicks make default, then the id of the photo is stored in the photo_id column of my profiles table.
The issue is redirecting them back to:
localhost:3000/settings/photo_gallery/:id
Is there a way I can redirect back to the photo album using the id of the photo that has just been set as the default? Can Rails find what album I want to redirect to just by looking at the id of a photo, as a photo belongs to a photo album, and a photo album has many photos?
I have the following tables in my database:
User(s): has one profile, has many PhotoAlbums
Profile(s): belongs to user
PhotoAlbum(s): belongs to user, has many photos
Photo(s): belongs to PhotoAlbum
Controller action:
def set_default_profile_photo
photo = Profile.find_by_user_id(current_user.id)
photo.photo_id = params[:photo_id]
photo.save
redirect_to "**HERE IS WHERE I'D LIKE TO REDIRECT TO THE PHOTOALBUM THE PHOTO IS IN**"
flash[:success] = "Default photo set!"
end
My routes:
users GET /users(.:format) {:action=>"index", :controller=>"users"}
POST /users(.:format) {:action=>"create", :controller=>"users"}
new_user GET /users/new(.:format) {:action=>"new", :controller=>"users"}
edit_user GET /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
user GET /users/:id(.:format) {:action=>"show", :controller=>"users"}
PUT /users/:id(.:format) {:action=>"update", :controller=>"users"}
DELETE /users/:id(.:format) {:action=>"destroy", :controller=>"users"}
sessions GET /sessions(.:format) {:action=>"index", :controller=>"sessions"}
POST /sessions(.:format) {:action=>"create", :controller=>"sessions"}
new_session GET /sessions/new(.:format) {:action=>"new", :controller=>"sessions"}
edit_session GET /sessions/:id/edit(.:format) {:action=>"edit", :controller=>"sessions"}
session GET /sessions/:id(.:format) {:action=>"show", :controller=>"sessions"}
PUT /sessions/:id(.:format) {:action=>"update", :controller=>"sessions"}
DELETE /sessions/:id(.:format) {:action=>"destroy", :controller=>"sessions"}
passwords GET /passwords(.:format) {:action=>"index", :controller=>"passwords"}
POST /passwords(.:format) {:action=>"create", :controller=>"passwords"}
new_password GET /passwords/new(.:format) {:action=>"new", :controller=>"passwords"}
edit_password GET /passwords/:id/edit(.:format) {:action=>"edit", :controller=>"passwords"}
password GET /passwords/:id(.:format) {:action=>"show", :controller=>"passwords"}
PUT /passwords/:id(.:format) {:action=>"update", :controller=>"passwords"}
DELETE /passwords/:id(.:format) {:action=>"destroy", :controller=>"passwords"}
profiles GET /profiles(.:format) {:action=>"index", :controller=>"profiles"}
POST /profiles(.:format) {:action=>"create", :controller=>"profiles"}
new_profile GET /profiles/new(.:format) {:action=>"new", :controller=>"profiles"}
edit_profile GET /profiles/:id/edit(.:format) {:action=>"edit", :controller=>"profiles"}
profile GET /profiles/:id(.:format) {:action=>"show", :controller=>"profiles"}
PUT /profiles/:id(.:format) {:action=>"update", :controller=>"profiles"}
DELETE /profiles/:id(.:format) {:action=>"destroy", :controller=>"profiles"}
emails GET /emails(.:format) {:action=>"index", :controller=>"emails"}
POST /emails(.:format) {:action=>"create", :controller=>"emails"}
new_email GET /emails/new(.:format) {:action=>"new", :controller=>"emails"}
edit_email GET /emails/:id/edit(.:format) {:action=>"edit", :controller=>"emails"}
email GET /emails/:id(.:format) {:action=>"show", :controller=>"emails"}
PUT /emails/:id(.:format) {:action=>"update", :controller=>"emails"}
DELETE /emails/:id(.:format) {:action=>"destroy", :controller=>"emails"}
root / {:controller=>"users", :action=>"new"}
success /success(.:format) {:action=>"success", :controller=>"users"}
login /login(.:format) {:action=>"new", :controller=>"sessions"}
logout /logout(.:format) {:action=>"destroy", :controller=>"sessions"}
reset_password /reset_password(.:format) {:action=>"new", :controller=>"passwords"}
setup_new_password /setup_new_password(.:format) {:action=>"edit", :controller=>"passwords"}
settings /settings(.:format) {:action=>"settings", :controller=>"users"}
settings_account /settings/account(.:format) {:controller=>"users", :action=>"account"}
settings_edit_profile /settings/edit_profile(.:format) {:controller=>"profiles", :action=>"edit_profile"}
/:username(.:format) {:controller=>"users", :action=>"show"}
change_password /change_password(.:format) {:action=>"change_password", :controller=>"users"}
profile_photo_set_default /profile_photo/set_default(.:format) {:controller=>"photo_albums", :action=>"set_default_profile_photo"}
album_photo_set_default /album_photo/set_default(.:format) {:controller=>"photo_albums", :action=>"set_default_album_photo"}
photo_albums GET /settings/photo_gallery(.:format) {:action=>"index", :controller=>"photo_albums"}
POST /settings/photo_gallery(.:format) {:action=>"create", :controller=>"photo_albums"}
new_photo_album GET /settings/photo_gallery/new(.:format) {:action=>"new", :controller=>"photo_albums"}
edit_photo_album GET /settings/photo_gallery/:id/edit(.:format) {:action=>"edit", :controller=>"photo_albums"}
photo_album GET /settings/photo_gallery/:id(.:format) {:action=>"show", :controller=>"photo_albums"}
PUT /settings/photo_gallery/:id(.:format) {:action=>"update", :controller=>"photo_albums"}
DELETE /settings/photo_gallery/:id(.:format) {:action=>"destroy", :controller=>"photo_albums"}
photos GET /settings/photo_gallery/photos(.:format) {:action=>"index", :controller=>"photos"}
POST /settings/photo_gallery/photos(.:format) {:action=>"create", :controller=>"photos"}
new_photo GET /settings/photo_gallery/photos/new(.:format) {:action=>"new", :controller=>"photos"}
edit_photo GET /settings/photo_gallery/photos/:id/edit(.:format) {:action=>"edit", :controller=>"photos"}
photo GET /settings/photo_gallery/photos/:id(.:format) {:action=>"show", :controller=>"photos"}
PUT /settings/photo_gallery/photos/:id(.:format) {:action=>"update", :controller=>"photos"}
DELETE /settings/photo_gallery/photos/:id(.:format) {:action=>"destroy", :controller=>"photos"}
This is what you want:
redirect_to request.referrer
For further reference:
http://en.wikipedia.org/wiki/HTTP_referer
redirect_to :back worked for me but I want to see if this was the right choice
http://api.rubyonrails.org/files/actionpack/lib/action_controller/metal/redirecting_rb.html
In Rails 5 it was introduced the function:
redirect_back(fallback_location: root_path)
It does redirect back whenever the HTTP_REFERER is known. Otherwise it redirects to the fallback_location.
The redirect_to :back is deprecated and will be removed from Rails 5.1.
In one project we used the session for temporary storage, because redirect_to :back did not work for us.
We had an def new where we set session[:return_to] = request.referer
in the def create we added redirect_to session[:return_to].
I do not know anymore, why we could not use redirect_to :back
If you came from the photo_album page, you should be able to do:
redirect_to :back
Otherwise, you should be able to do a named route like:
redirect_to photo_album_path(photo.album_id) # or whatever the association key is
BTW, why do you have photo_albums mapping to photo_galleries? It's a lot less confusing if you named your resources and routes in a similiar manner. ie: if you wanted your route endpoints to use /photo_galleries you should name your resource PhotoGallery.

Rails 3.1 - Devise - CanCan: "../users/sign_out" gets routing error "uninitialized constant UsersController"

"sign_in" works fine but clicking on "sign_out" link locks up during routing (see routes.rb below).
Not sure where to proceed. I'm using RubyMine (IDE) debugger.
I click on a link from layout/applications.html.erb:
<%= link_to('Logout', destroy_user_session_path) %>
RM Debugger watchlist shows: destroy_user_session_path="/user/sign_out"
When I sign_in as a user I breakpoint in "../devise/sessions_controller.rb#sign_in" and that all works
fine when I continue.
The status change gives me a "sign_out" link in my applications.html layout but when I click
there I get above routine error. I don't get breakpoints in "../application_controller.rb"
or "../devise/sessions_controller.rb#sign_out"
Here is routes.rb
Demo::Application.routes.draw do
# replace devise_for :users with:
devise_for :users, :controllers => { :registrations => "devise/registrations" }
get "user/show"
get "user/edit"
get "user/index"
get "user/create"
get "user/update"
get "user/new"
resources :users
resources :orders
resources :carts
resources :line_items
resource :store do
member do
get "store/index"
end
end
match ':controller(/:action(/:id(.:format)))'
root to: 'store#index'
end
And rake:routes
new_user_session GET /users/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
user_session POST /users/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_user_session DELETE /users/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
cancel_user_registration GET /users/cancel(.:format) {:action=>"cancel", :controller=>"devise/registrations"}
user_registration POST /users(.:format) {:action=>"create", :controller=>"devise/registrations"}
new_user_registration GET /users/sign_up(.:format) {:action=>"new", :controller=>"devise/registrations"}
edit_user_registration GET /users/edit(.:format) {:action=>"edit", :controller=>"devise/registrations"}
PUT /users(.:format) {:action=>"update", :controller=>"devise/registrations"}
DELETE /users(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
user_show GET /user/show(.:format) {:controller=>"user", :action=>"show"}
user_edit GET /user/edit(.:format) {:controller=>"user", :action=>"edit"}
user_index GET /user/index(.:format) {:controller=>"user", :action=>"index"}
user_create GET /user/create(.:format) {:controller=>"user", :action=>"create"}
user_update GET /user/update(.:format) {:controller=>"user", :action=>"update"}
user_new GET /user/new(.:format) {:controller=>"user", :action=>"new"}
users GET /users(.:format) {:action=>"index", :controller=>"users"}
POST /users(.:format) {:action=>"create", :controller=>"users"}
new_user GET /users/new(.:format) {:action=>"new", :controller=>"users"}
edit_user GET /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
user GET /users/:id(.:format) {:action=>"show", :controller=>"users"}
PUT /users/:id(.:format) {:action=>"update", :controller=>"users"}
DELETE /users/:id(.:format) {:action=>"destroy", :controller=>"users"}
orders GET /orders(.:format) {:action=>"index", :controller=>"orders"}
POST /orders(.:format) {:action=>"create", :controller=>"orders"}
new_order GET /orders/new(.:format) {:action=>"new", :controller=>"orders"}
edit_order GET /orders/:id/edit(.:format) {:action=>"edit", :controller=>"orders"}
order GET /orders/:id(.:format) {:action=>"show", :controller=>"orders"}
PUT /orders/:id(.:format) {:action=>"update", :controller=>"orders"}
DELETE /orders/:id(.:format) {:action=>"destroy", :controller=>"orders"}
carts GET /carts(.:format) {:action=>"index", :controller=>"carts"}
POST /carts(.:format) {:action=>"create", :controller=>"carts"}
new_cart GET /carts/new(.:format) {:action=>"new", :controller=>"carts"}
edit_cart GET /carts/:id/edit(.:format) {:action=>"edit", :controller=>"carts"}
cart GET /carts/:id(.:format) {:action=>"show", :controller=>"carts"}
PUT /carts/:id(.:format) {:action=>"update", :controller=>"carts"}
DELETE /carts/:id(.:format) {:action=>"destroy", :controller=>"carts"}
line_items GET /line_items(.:format) {:action=>"index", :controller=>"line_items"}
POST /line_items(.:format) {:action=>"create", :controller=>"line_items"}
new_line_item GET /line_items/new(.:format) {:action=>"new", :controller=>"line_items"}
edit_line_item GET /line_items/:id/edit(.:format) {:action=>"edit", :controller=>"line_items"}
line_item GET /line_items/:id(.:format) {:action=>"show", :controller=>"line_items"}
PUT /line_items/:id(.:format) {:action=>"update", :controller=>"line_items"}
DELETE /line_items/:id(.:format) {:action=>"destroy", :controller=>"line_items"}
store_index_store GET /store/store/index(.:format) {:controller=>"store/store", :action=>"index"}
store POST /store(.:format) {:action=>"create", :controller=>"stores"}
new_store GET /store/new(.:format) {:action=>"new", :controller=>"stores"}
edit_store GET /store/edit(.:format) {:action=>"edit", :controller=>"stores"}
GET /store(.:format) {:action=>"show", :controller=>"stores"}
PUT /store(.:format) {:action=>"update", :controller=>"stores"}
DELETE /store(.:format) {:action=>"destroy", :controller=>"stores"}
/:controller(/:action(/:id(.:format)))
root / {:controller=>"store", :action=>"index"}
Robin
Change your link to
<%= link_to('Logout', destroy_user_session_path, :method=>'delete') %>
e.g. add `:method=>'delete'

Weird Routing Issue - No Method Error

I'm getting a no method error when loading form for a nested resource. I have a fix for it but it's not right I'm sure.
My routes.rb contains:
resources :orders do
resources :comments, :technicals, :milestones
end
At the top of my techncials.html.erb form I have this:
<%= semantic_form_for([#order, #technical]) do |f| %>
When loading the form, I get an error:
> NoMethodError in Technicals#new
>
> undefined method `technicals_path' for
> #<#<Class:0x00000102118318>:0x000001020ec470>
If I then put this in my routes, it all works just fine:
resources :technicals, :only => [ :create ]
What on earth am I doing wrong, if anything??!
Edit: I think My models etc. are all set up correctly:
class Technical < ActiveRecord::Base
belongs_to :order
end
-- edit --
company_order_comments GET /companies/:company_id/orders/:order_id/comments(.:format) {:action=>"index", :controller=>"comments"}
POST /companies/:company_id/orders/:order_id/comments(.:format) {:action=>"create", :controller=>"comments"}
new_company_order_comment GET /companies/:company_id/orders/:order_id/comments/new(.:format) {:action=>"new", :controller=>"comments"}
edit_company_order_comment GET /companies/:company_id/orders/:order_id/comments/:id/edit(.:format) {:action=>"edit", :controller=>"comments"}
company_order_comment GET /companies/:company_id/orders/:order_id/comments/:id(.:format) {:action=>"show", :controller=>"comments"}
PUT /companies/:company_id/orders/:order_id/comments/:id(.:format) {:action=>"update", :controller=>"comments"}
DELETE /companies/:company_id/orders/:order_id/comments/:id(.:format) {:action=>"destroy", :controller=>"comments"}
company_orders GET /companies/:company_id/orders(.:format) {:action=>"index", :controller=>"orders"}
POST /companies/:company_id/orders(.:format) {:action=>"create", :controller=>"orders"}
new_company_order GET /companies/:company_id/orders/new(.:format) {:action=>"new", :controller=>"orders"}
edit_company_order GET /companies/:company_id/orders/:id/edit(.:format) {:action=>"edit", :controller=>"orders"}
company_order GET /companies/:company_id/orders/:id(.:format) {:action=>"show", :controller=>"orders"}
PUT /companies/:company_id/orders/:id(.:format) {:action=>"update", :controller=>"orders"}
DELETE /companies/:company_id/orders/:id(.:format) {:action=>"destroy", :controller=>"orders"}
companies GET /companies(.:format) {:action=>"index", :controller=>"companies"}
POST /companies(.:format) {:action=>"create", :controller=>"companies"}
new_company GET /companies/new(.:format) {:action=>"new", :controller=>"companies"}
edit_company GET /companies/:id/edit(.:format) {:action=>"edit", :controller=>"companies"}
company GET /companies/:id(.:format) {:action=>"show", :controller=>"companies"}
PUT /companies/:id(.:format) {:action=>"update", :controller=>"companies"}
DELETE /companies/:id(.:format) {:action=>"destroy", :controller=>"companies"}
order_comments GET /orders/:order_id/comments(.:format) {:action=>"index", :controller=>"comments"}
POST /orders/:order_id/comments(.:format) {:action=>"create", :controller=>"comments"}
new_order_comment GET /orders/:order_id/comments/new(.:format) {:action=>"new", :controller=>"comments"}
edit_order_comment GET /orders/:order_id/comments/:id/edit(.:format) {:action=>"edit", :controller=>"comments"}
order_comment GET /orders/:order_id/comments/:id(.:format) {:action=>"show", :controller=>"comments"}
PUT /orders/:order_id/comments/:id(.:format) {:action=>"update", :controller=>"comments"}
DELETE /orders/:order_id/comments/:id(.:format) {:action=>"destroy", :controller=>"comments"}
order_technicals GET /orders/:order_id/technicals(.:format) {:action=>"index", :controller=>"technicals"}
POST /orders/:order_id/technicals(.:format) {:action=>"create", :controller=>"technicals"}
new_order_technical GET /orders/:order_id/technicals/new(.:format) {:action=>"new", :controller=>"technicals"}
edit_order_technical GET /orders/:order_id/technicals/:id/edit(.:format) {:action=>"edit", :controller=>"technicals"}
order_technical GET /orders/:order_id/technicals/:id(.:format) {:action=>"show", :controller=>"technicals"}
PUT /orders/:order_id/technicals/:id(.:format) {:action=>"update", :controller=>"technicals"}
DELETE /orders/:order_id/technicals/:id(.:format) {:action=>"destroy", :controller=>"technicals"}
order_milestones GET /orders/:order_id/milestones(.:format) {:action=>"index", :controller=>"milestones"}
POST /orders/:order_id/milestones(.:format) {:action=>"create", :controller=>"milestones"}
new_order_milestone GET /orders/:order_id/milestones/new(.:format) {:action=>"new", :controller=>"milestones"}
edit_order_milestone GET /orders/:order_id/milestones/:id/edit(.:format) {:action=>"edit", :controller=>"milestones"}
order_milestone GET /orders/:order_id/milestones/:id(.:format) {:action=>"show", :controller=>"milestones"}
PUT /orders/:order_id/milestones/:id(.:format) {:action=>"update", :controller=>"milestones"}
DELETE /orders/:order_id/milestones/:id(.:format) {:action=>"destroy", :controller=>"milestones"}
orders GET /orders(.:format) {:action=>"index", :controller=>"orders"}
POST /orders(.:format) {:action=>"create", :controller=>"orders"}
new_order GET /orders/new(.:format) {:action=>"new", :controller=>"orders"}
edit_order GET /orders/:id/edit(.:format) {:action=>"edit", :controller=>"orders"}
order GET /orders/:id(.:format) {:action=>"show", :controller=>"orders"}
PUT /orders/:id(.:format) {:action=>"update", :controller=>"orders"}
DELETE /orders/:id(.:format) {:action=>"destroy", :controller=>"orders"}
/orders(.:format) {:controller=>"orders", :action=>"index"}
root /(.:format) {:controller=>"home", :action=>"index"}
NoMethodError in Technicals#new
Does controllers/technicals_controller.rb have a "def new"?
If not, try adding one:
def new
#technical = Technical.new
end

Singular resources namespaced by its slug (Routing)

I have the followed Rails 3 routes:
Hello::Application.routes.draw do
resources :blogs do
resources :articles do
resources :comments
end
end
end
By raking them, we can find:
GET /blogs/:blog_id/articles/:article_id/comments(.:format) {:action=>"index", :controller=>"comments"}
blog_article_comments POST /blogs/:blog_id/articles/:article_id/comments(.:format) {:action=>"create", :controller=>"comments"}
new_blog_article_comment GET /blogs/:blog_id/articles/:article_id/comments/new(.:format) {:action=>"new", :controller=>"comments"}
GET /blogs/:blog_id/articles/:article_id/comments/:id(.:format) {:action=>"show", :controller=>"comments"}
PUT /blogs/:blog_id/articles/:article_id/comments/:id(.:format) {:action=>"update", :controller=>"comments"}
blog_article_comment DELETE /blogs/:blog_id/articles/:article_id/comments/:id(.:format) {:action=>"destroy", :controller=>"comments"}
edit_blog_article_comment GET /blogs/:blog_id/articles/:article_id/comments/:id/edit(.:format) {:action=>"edit", :controller=>"comments"}
GET /blogs/:blog_id/articles(.:format) {:action=>"index", :controller=>"articles"}
blog_articles POST /blogs/:blog_id/articles(.:format) {:action=>"create", :controller=>"articles"}
new_blog_article GET /blogs/:blog_id/articles/new(.:format) {:action=>"new", :controller=>"articles"}
GET /blogs/:blog_id/articles/:id(.:format) {:action=>"show", :controller=>"articles"}
PUT /blogs/:blog_id/articles/:id(.:format) {:action=>"update", :controller=>"articles"}
blog_article DELETE /blogs/:blog_id/articles/:id(.:format) {:action=>"destroy", :controller=>"articles"}
edit_blog_article GET /blogs/:blog_id/articles/:id/edit(.:format) {:action=>"edit", :controller=>"articles"}
GET /blogs(.:format) {:action=>"index", :controller=>"blogs"}
blogs POST /blogs(.:format) {:action=>"create", :controller=>"blogs"}
new_blog GET /blogs/new(.:format) {:action=>"new", :controller=>"blogs"}
GET /blogs/:id(.:format) {:action=>"show", :controller=>"blogs"}
PUT /blogs/:id(.:format) {:action=>"update", :controller=>"blogs"}
blog DELETE /blogs/:id(.:format) {:action=>"destroy", :controller=>"blogs"}
edit_blog GET /blogs/:id/edit(.:format) {:action=>"edit", :controller=>"blogs"}
Because every routes begin with the same root path (/blogs), I would like to shorten addresses by removing it (when :blog_id is given).
In this why, I could have thoses routes (I think it's more DRY):
GET /:blog_id/articles/:article_id/comments(.:format) {:action=>"index", :controller=>"comments"}
blog_article_comments POST /:blog_id/articles/:article_id/comments(.:format) {:action=>"create", :controller=>"comments"}
new_blog_article_comment GET /:blog_id/articles/:article_id/comments/new(.:format) {:action=>"new", :controller=>"comments"}
GET /:blog_id/articles/:article_id/comments/:id(.:format) {:action=>"show", :controller=>"comments"}
PUT /:blog_id/articles/:article_id/comments/:id(.:format) {:action=>"update", :controller=>"comments"}
blog_article_comment DELETE /:blog_id/articles/:article_id/comments/:id(.:format) {:action=>"destroy", :controller=>"comments"}
edit_blog_article_comment GET /:blog_id/articles/:article_id/comments/:id/edit(.:format) {:action=>"edit", :controller=>"comments"}
GET /:blog_id/articles(.:format) {:action=>"index", :controller=>"articles"}
blog_articles POST /:blog_id/articles(.:format) {:action=>"create", :controller=>"articles"}
new_blog_article GET /:blog_id/articles/new(.:format) {:action=>"new", :controller=>"articles"}
GET /:blog_id/articles/:id(.:format) {:action=>"show", :controller=>"articles"}
PUT /:blog_id/articles/:id(.:format) {:action=>"update", :controller=>"articles"}
blog_article DELETE /:blog_id/articles/:id(.:format) {:action=>"destroy", :controller=>"articles"}
edit_blog_article GET /:blog_id/articles/:id/edit(.:format) {:action=>"edit", :controller=>"articles"}
GET /blogs(.:format) {:action=>"index", :controller=>"blogs"}
blogs POST /blogs(.:format) {:action=>"create", :controller=>"blogs"}
new_blog GET /blogs/new(.:format) {:action=>"new", :controller=>"blogs"}
GET /blogs/:id(.:format) {:action=>"show", :controller=>"blogs"}
PUT /blogs/:id(.:format) {:action=>"update", :controller=>"blogs"}
blog DELETE /blogs/:id(.:format) {:action=>"destroy", :controller=>"blogs"}
edit_blog GET /blogs/:id/edit(.:format) {:action=>"edit", :controller=>"blogs"}
According to you, what kind of change I should make over my current routes configuration?
Thanks.
My opinion is that you need to look at shallow routes. To my knowledge the customisation you are asking is not achievable using route resources and I don't think it's desirable.
Where you have defined one to many relationships you do not need to pass the identifier for each of the nested resources. This is considered bad practice (by some, though not others). For example, instead of this long url:
/blog/:blog_id/articles/:article_id/comments/:id
#controller
#blog = Blog.find params[:blog_id]
#article = Blog.find params[:article_id]
#comment = Comment.find params[:id]
you actually need only:
/comments/:id
#controller
#comment = Comment.find params[:id]
#article = #comment.article
#blog = #article.blog
More info:
http://railscasts.com/episodes/139-nested-resources
"Resources should never be nested more than 1 level deep." (http://guides.rubyonrails.org/routing.html)
If the Rails documentation is making this strong of a recommendation I have no idea why it is allowed still.

Uninitialized constant problem for Rails routes

Here's my route configuration:
map.resources :services do |services|
services.resources :capabilities do |capabilities|
capabilities.resources :http_headers
end
end
Here's my "rake routes" output:
laran:trunk laran$ rake routes
(in /Users/laran/workspace/kibo/mega/server/trunk)
accounts GET /accounts(.:format) {:action=>"index", :controller=>"accounts"}
POST /accounts(.:format) {:action=>"create", :controller=>"accounts"}
new_account GET /accounts/new(.:format) {:action=>"new", :controller=>"accounts"}
edit_account GET /accounts/:id/edit(.:format) {:action=>"edit", :controller=>"accounts"}
account GET /accounts/:id(.:format) {:action=>"show", :controller=>"accounts"}
PUT /accounts/:id(.:format) {:action=>"update", :controller=>"accounts"}
DELETE /accounts/:id(.:format) {:action=>"destroy", :controller=>"accounts"}
services GET /services(.:format) {:action=>"index", :controller=>"services"}
POST /services(.:format) {:action=>"create", :controller=>"services"}
new_service GET /services/new(.:format) {:action=>"new", :controller=>"services"}
edit_service GET /services/:id/edit(.:format) {:action=>"edit", :controller=>"services"}
service GET /services/:id(.:format) {:action=>"show", :controller=>"services"}
PUT /services/:id(.:format) {:action=>"update", :controller=>"services"}
DELETE /services/:id(.:format) {:action=>"destroy", :controller=>"services"}
service_capabilities GET /services/:service_id/capabilities(.:format) {:action=>"index", :controller=>"capabilities"}
POST /services/:service_id/capabilities(.:format) {:action=>"create", :controller=>"capabilities"}
new_service_capability GET /services/:service_id/capabilities/new(.:format) {:action=>"new", :controller=>"capabilities"}
edit_service_capability GET /services/:service_id/capabilities/:id/edit(.:format) {:action=>"edit", :controller=>"capabilities"}
service_capability GET /services/:service_id/capabilities/:id(.:format) {:action=>"show", :controller=>"capabilities"}
PUT /services/:service_id/capabilities/:id(.:format) {:action=>"update", :controller=>"capabilities"}
DELETE /services/:service_id/capabilities/:id(.:format) {:action=>"destroy", :controller=>"capabilities"}
service_capability_http_headers GET /services/:service_id/capabilities/:capability_id/http_headers(.:format) {:action=>"index", :controller=>"http_headers"}
POST /services/:service_id/capabilities/:capability_id/http_headers(.:format) {:action=>"create", :controller=>"http_headers"}
new_service_capability_http_header GET /services/:service_id/capabilities/:capability_id/http_headers/new(.:format) {:action=>"new", :controller=>"http_headers"}
edit_service_capability_http_header GET /services/:service_id/capabilities/:capability_id/http_headers/:id/edit(.:format) {:action=>"edit", :controller=>"http_headers"}
service_capability_http_header GET /services/:service_id/capabilities/:capability_id/http_headers/:id(.:format) {:action=>"show", :controller=>"http_headers"}
PUT /services/:service_id/capabilities/:capability_id/http_headers/:id(.:format) {:action=>"update", :controller=>"http_headers"}
DELETE /services/:service_id/capabilities/:capability_id/http_headers/:id(.:format) {:action=>"destroy", :controller=>"http_headers"}
/login {:action=>"login", :controller=>"accounts"}
/logout {:action=>"logout", :controller=>"accounts"}
root / {:action=>"index", :controller=>"default"}
laran:trunk laran$
When I go to /services/new though, I get this error:
NameError in ServicesController#new
uninitialized constant ServicesController::Services
What gives? How can I get things working and routed correctly? Thanks.
Is ServicesController backed up by a model Service? Did you accidentally reference it as Services in your controller?
This probably doesn't have anything to do with your routes; your new method in ServicesController is trying to use a (class? constant? object?) named Services that doesn't exist.

Resources