How to re-name routes? - ruby-on-rails

I have nested routes on my site for Sections and Pages.
resources :sections do
resources :pages
end
This is a sample URL:
sitename.com/sections/5/pages/22
I don't like the name 'sections', and would prefer 'chapters'.
sitename.com/chapters/5/pages/22
I assume re-naming the model would be to complicated, so how can I just re-name the route easily?

Pass your desired URL segment name as the value to the path argument:
resources :sections, :path => :chapters do
resources :pages
end
This results the the following routes:
section_pages GET /chapters/:section_id/pages(.:format) pages#index
POST /chapters/:section_id/pages(.:format) pages#create
new_section_page GET /chapters/:section_id/pages/new(.:format) pages#new
edit_section_page GET /chapters/:section_id/pages/:id/edit(.:format) pages#edit
section_page GET /chapters/:section_id/pages/:id(.:format) pages#show
PUT /chapters/:section_id/pages/:id(.:format) pages#update
DELETE /chapters/:section_id/pages/:id(.:format) pages#destroy
sections GET /chapters(.:format) sections#index
POST /chapters(.:format) sections#create
new_section GET /chapters/new(.:format) sections#new
edit_section GET /chapters/:id/edit(.:format) sections#edit
section GET /chapters/:id(.:format) sections#show
PUT /chapters/:id(.:format) sections#update
DELETE /chapters/:id(.:format) sections#destroy

Related

Nested routes without resources

I have an application with an object, movies, that doesn't use some of the standard RESTful routes. I don't want the 'new' route to lead anywhere.
The problem is I have 'movies' with a nested resource 'reviews'
resources :movies do
resources :reviews
end
I want this style of routing:
get '/movies', to: "movies#index"
But with nested routes. Is this possible? I'm sure there's answer to this somewhere on this site, but I can't find it.
You can simply do:
resources :movies, :only => [:index] do
resources :reviews
end
Which will give you:
movie_reviews GET /movies/:movie_id/reviews(.:format) reviews#index
POST /movies/:movie_id/reviews(.:format) reviews#create
new_movie_review GET /movies/:movie_id/reviews/new(.:format) reviews#new
edit_movie_review GET /movies/:movie_id/reviews/:id/edit(.:format) reviews#edit
movie_review GET /movies/:movie_id/reviews/:id(.:format) reviews#show
PATCH /movies/:movie_id/reviews/:id(.:format) reviews#update
PUT /movies/:movie_id/reviews/:id(.:format) reviews#update
DELETE /movies/:movie_id/reviews/:id(.:format) reviews#destroy
movies GET /movies(.:format) movies#index

Restful url not picking up id

I am having troubles with routes. I have defined routes like this:
resource :demo
resource :subjects
resource :pages
resource :sections
when i do
rake routes
it doesn't show right urls. it shows something like
Prefix Verb URI Pattern Controller#Action
root GET / demo#index
demo POST /demo(.:format) demos#create
new_demo GET /demo/new(.:format) demos#new
edit_demo GET /demo/edit(.:format) demos#edit
GET /demo(.:format) demos#show
PATCH /demo(.:format) demos#update
PUT /demo(.:format) demos#update
DELETE /demo(.:format) demos#destroy
subjects POST /subjects(.:format) subjects#create
new_subjects GET /subjects/new(.:format) subjects#new
edit_subjects GET /subjects/edit(.:format) subjects#edit
GET /subjects(.:format) subjects#show
PATCH /subjects(.:format) subjects#update
PUT /subjects(.:format) subjects#update
DELETE /subjects(.:format) subjects#destroy
pages POST /pages(.:format) pages#create
new_pages GET /pages/new(.:format) pages#new
edit_pages GET /pages/edit(.:format) pages#edit
GET /pages(.:format) pages#show
PATCH /pages(.:format) pages#update
PUT /pages(.:format) pages#update
DELETE /pages(.:format) pages#destroy
sections POST /sections(.:format) sections#create
new_sections GET /sections/new(.:format) sections#new
edit_sections GET /sections/edit(.:format) sections#edit
GET /sections(.:format) sections#show
PATCH /sections(.:format) sections#update
PUT /sections(.:format) sections#update
DELETE /sections(.:format) sections#destroy
GET /:controller(/:action(/:id(.:format))) :controller#:action
none of the urls has :id in them. what i might be doing wrong? It still sends id to controller but i am having hard time calling index and show methods as both of them are mapped to -----#show
This is because you are using singular resource, e.g. resource :foo. When you use singular resource, you don't get the :id. In order to get the :id in the parameter, you should change the resources declarations to plural resources:
resources :demoes
resources :subjects
resources :pages
resources :sections

Use same controller for multiple routes?

Is there a way to write the following routes so you don't have to specify the same controller each time?...
get 'jobs' => 'pages#jobs'
get 'contact' => 'pages#contact'
get 'terms' => 'pages#terms'
get 'privacy' => 'pages#privacy'
Here are couple of alternatives:
Out of the three, the first one i.e., Using scope as "/" would create the exact same routes as the ones created by the routes defined in the question.
1. Using scope as "/"
scope "/", controller: :pages do
get 'jobs'
get 'contact'
get 'terms'
get 'privacy'
end
Creates routes as below:
jobs GET /jobs(.:format) pages#jobs
contact GET /contact(.:format) pages#contact
terms GET /terms(.:format) pages#terms
privacy GET /privacy(.:format) pages#privacy
2. Using Scope as "pages"
scope :pages, controller: :pages do
get 'jobs'
get 'contact'
get 'terms'
get 'privacy'
end
Creates routes as below:
jobs GET /pages/jobs(.:format) pages#jobs
contact GET /pages/contact(.:format) pages#contact
terms GET /pages/terms(.:format) pages#terms
privacy GET /pages/privacy(.:format) pages#privacy
3. Nesting routes
resources :pages do
member do
get 'jobs'
get 'contact'
get 'terms'
get 'privacy'
end
end
Creates routes as below:
jobs_page GET /pages/:id/jobs(.:format) pages#jobs
contact_page GET /pages/:id/contact(.:format) pages#contact
terms_page GET /pages/:id/terms(.:format) pages#terms
privacy_page GET /pages/:id/privacy(.:format) pages#privacy

Nested routes work but others don't

Struggling with routing in Rails!
This works: http://127.0.0.1:3000/locations/1/statistics but http://127.0.0.1:3000/locations/ does not work.
My routes look like this:
resources :locations do
resources :statistics
end
I can get only http://127.0.0.1:3000/locations/ working if I just do
resources locations
but then the nested routes don't work!
How can I get both working?
Many thanks.
EDIT rake routes:
location_statistics GET /locations/:location_id/statistics(.:format) statistics#index
POST /locations/:location_id/statistics(.:format) statistics#create
new_location_statistic GET /locations/:location_id/statistics/new(.:format) statistics#new
edit_location_statistic GET /locations/:location_id/statistics/:id/edit(.:format) statistics#edit
location_statistic GET /locations/:location_id/statistics/:id(.:format) statistics#show
PUT /locations/:location_id/statistics/:id(.:format) statistics#update
DELETE /locations/:location_id/statistics/:id(.:format) statistics#destroy
locations GET /locations(.:format) locations#index
POST /locations(.:format) locations#create
new_location GET /locations/new(.:format) locations#new
edit_location GET /locations/:id/edit(.:format) locations#edit
location GET /locations/:id(.:format) locations#show
PUT /locations/:id(.:format) locations#update
DELETE /locations/:id(.:format) locations#destroy
home_index GET /home/index(.:format) home#index
about /about(.:format) home#about
contact /contact(.:format) home#contact
root / home#index
EDIT 2 routes file
match '/about/' => 'home#about'
match '/contact/' => 'home#contact'
resources :locations do
resources :statistics
end
get "home/index"
EDIT 3
My actual error:
Routing Error
No route matches {:controller=>"statistics", :location_id=>nil}
when I go to http://127.0.0.1:3000/locations/
You should either use
=link_to "Locations", locations_path
or
# get sure #location is not nil
=link_to "Location Statistics", location_statistics_path(#location)

Changing every route from /product to /eshop in Rails Route 3

in my routes.rb i have foll entries:-
resources :products do
get 'get_products', :on => :collection
get 'show_product_details',:on=>:member
get 'buy_this',:on=>:collection
get 'search_product',:on=>:collection
end
i want to change every /products to /eshop in the url.
i am not sure but can i use :path=>:eshop.Will it will be also applicable to the sub routes as well such as eshop/get_products,eshop/buy_this...etc.
You can modify your routes and run rake routes in the terminal to check the paths.
resources :products, :path => 'eshop', :as => 'eshop' do
get 'get_products', :on => :collection
get 'show_product_details',:on=>:member
get 'buy_this',:on=>:collection
get 'search_product',:on=>:collection
end
will produce these
get_products_eshop_index GET /eshop/get_products(.:format) products#get_products
show_product_details_eshop GET /eshop/:id/show_product_details(.:format) products#show_product_details
buy_this_eshop_index GET /eshop/buy_this(.:format) products#buy_this
search_product_eshop_index GET /eshop/search_product(.:format) products#search_product
eshop_index GET /eshop(.:format) products#index
POST /eshop(.:format) products#create
new_eshop GET /eshop/new(.:format) products#new
edit_eshop GET /eshop/:id/edit(.:format) products#edit
eshop GET /eshop/:id(.:format) products#show
PUT /eshop/:id(.:format) products#update
DELETE /eshop/:id(.:format) products#destroy

Resources