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
Related
I have the following routes declared:
resources :accounts, param: :account_id do
resources :instructions, param: :instruction_id do
resources :messages, param: :message_id
end
end
So accounts, instructions, messages are the 3 models I have. This gives me the routes:
account_instruction_messages GET /accounts/:account_account_id/instructions/:instruction_instruction_id/messages(.:format) messages#index
POST /accounts/:account_account_id/instructions/:instruction_instruction_id/messages(.:format) messages#create
new_account_instruction_message GET /accounts/:account_account_id/instructions/:instruction_instruction_id/messages/new(.:format) messages#new
edit_account_instruction_message GET /accounts/:account_account_id/instructions/:instruction_instruction_id/messages/:message_id/edit(.:format) messages#edit
account_instruction_message GET /accounts/:account_account_id/instructions/:instruction_instruction_id/messages/:message_id(.:format) messages#show
PATCH /accounts/:account_account_id/instructions/:instruction_instruction_id/messages/:message_id(.:format) messages#update
PUT /accounts/:account_account_id/instructions/:instruction_instruction_id/messages/:message_id(.:format) messages#update
DELETE /accounts/:account_account_id/instructions/:instruction_instruction_id/messages/:message_id(.:format) messages#destroy
account_instructions GET /accounts/:account_account_id/instructions(.:format) instructions#index
POST /accounts/:account_account_id/instructions(.:format) instructions#create
new_account_instruction GET /accounts/:account_account_id/instructions/new(.:format) instructions#new
edit_account_instruction GET /accounts/:account_account_id/instructions/:instruction_id/edit(.:format) instructions#edit
account_instruction GET /accounts/:account_account_id/instructions/:instruction_id(.:format) instructions#show
PATCH /accounts/:account_account_id/instructions/:instruction_id(.:format) instructions#update
PUT /accounts/:account_account_id/instructions/:instruction_id(.:format) instructions#update
DELETE /accounts/:account_account_id/instructions/:instruction_id(.:format) instructions#destroy
That looks wrong to me, I was expecting
/accounts/:account_id/instructions/:instruction_id etc...?
Can someone advise what I am doing wrong?
you can do it in next way:
resources :accounts do
resources :instructions do
resources :messages, param: :message_id
end
end
this will make you next routes:
account_instruction_messages GET /accounts/:account_id/instructions/:instruction_id/messages(.:format) messages#index
POST /accounts/:account_id/instructions/:instruction_id/messages(.:format) messages#create
new_account_instruction_message GET /accounts/:account_id/instructions/:instruction_id/messages/new(.:format) messages#new
edit_account_instruction_message GET /accounts/:account_id/instructions/:instruction_id/messages/:message_id/edit(.:format) messages#edit
account_instruction_message GET /accounts/:account_id/instructions/:instruction_id/messages/:message_id(.:format) messages#show
PATCH /accounts/:account_id/instructions/:instruction_id/messages/:message_id(.:format) messages#update
PUT /accounts/:account_id/instructions/:instruction_id/messages/:message_id(.:format) messages#update
DELETE /accounts/:account_id/instructions/:instruction_id/messages/:message_id(.:format) messages#destroy
account_instructions GET /accounts/:account_id/instructions(.:format) instructions#index
POST /accounts/:account_id/instructions(.:format) instructions#create
new_account_instruction GET /accounts/:account_id/instructions/new(.:format) instructions#new
edit_account_instruction GET /accounts/:account_id/instructions/:id/edit(.:format) instructions#edit
account_instruction GET /accounts/:account_id/instructions/:id(.:format) instructions#show
PATCH /accounts/:account_id/instructions/:id(.:format) instructions#update
PUT /accounts/:account_id/instructions/:id(.:format) instructions#update
DELETE /accounts/:account_id/instructions/:id(.:format) instructions#destroy
accounts GET /accounts(.:format) accounts#index
POST /accounts(.:format) accounts#create
new_account GET /accounts/new(.:format) accounts#new
edit_account GET /accounts/:id/edit(.:format) accounts#edit
account GET /accounts/:id(.:format) accounts#show
PATCH /accounts/:id(.:format) accounts#update
PUT /accounts/:id(.:format) accounts#update
DELETE /accounts/:id(.:format) accounts#destroy
UPDATE
but if you need all same params, you can do:
resources :accounts, only: [] do
resources :instructions, only: [] do
resources :messages, param: :message_id
end
end
resources :accounts, param: :account_id
resources :instructions, param: :instruction_id
and this will return:
account_instruction_messages GET /accounts/:account_id/instructions/:instruction_id/messages(.:format) messages#index
POST /accounts/:account_id/instructions/:instruction_id/messages(.:format) messages#create
new_account_instruction_message GET /accounts/:account_id/instructions/:instruction_id/messages/new(.:format) messages#new
edit_account_instruction_message GET /accounts/:account_id/instructions/:instruction_id/messages/:message_id/edit(.:format) messages#edit
account_instruction_message GET /accounts/:account_id/instructions/:instruction_id/messages/:message_id(.:format) messages#show
PATCH /accounts/:account_id/instructions/:instruction_id/messages/:message_id(.:format) messages#update
PUT /accounts/:account_id/instructions/:instruction_id/messages/:message_id(.:format) messages#update
DELETE /accounts/:account_id/instructions/:instruction_id/messages/:message_id(.:format) messages#destroy
accounts GET /accounts(.:format) accounts#index
POST /accounts(.:format) accounts#create
new_account GET /accounts/new(.:format) accounts#new
edit_account GET /accounts/:accounts_id/edit(.:format) accounts#edit
account GET /accounts/:accounts_id(.:format) accounts#show
PATCH /accounts/:accounts_id(.:format) accounts#update
PUT /accounts/:accounts_id(.:format) accounts#update
DELETE /accounts/:accounts_id(.:format) accounts#destroy
....... and so on
I want to create rails routes by hand rather than using the automatic resource :tasks (it's just an exercise, I'll go back to the using resources :tasks once I understand this).
I /think/ that I have the right syntax, but the prefixes (path_helpers) generated when I write the routes by hand are wrong. Why, and what am I not doing properly?
Here is my code:
Rails.application.routes.draw do
root to: 'tasks#index'
get 'tasks', to: 'tasks#index'
get 'tasks/:id', to: 'tasks#show'
get 'tasks/new', to: 'tasks#new'
post 'tasks', to: 'tasks#create'
get 'tasks/:id/edit', to: 'tasks#edit'
patch 'tasks/:id', to: 'tasks#update'
delete 'tasks/:id', to: 'tasks#destroy'
end
Here are the routes & prefixes it creates when I call rails routes in my terminal:
Prefix Verb URI Pattern Controller#Action
root GET / tasks#index
tasks GET /tasks(.:format) tasks#index
GET /tasks/:id(.:format) tasks#show
tasks_new GET /tasks/new(.:format) tasks#new
POST /tasks(.:format) tasks#create
GET /tasks/:id/edit(.:format) tasks#edit
PATCH /tasks/:id(.:format) tasks#update
DELETE /tasks/:id(.:format) tasks#destroy
Here is what I get when I use resource :tasks
Prefix Verb URI Pattern Controller#Action
tasks GET /tasks(.:format) tasks#index
POST /tasks(.:format) tasks#create
new_task GET /tasks/new(.:format) tasks#new
edit_task GET /tasks/:id/edit(.:format) tasks#edit
task GET /tasks/:id(.:format) tasks#show
PATCH /tasks/:id(.:format) tasks#update
PUT /tasks/:id(.:format) tasks#update
DELETE /tasks/:id(.:format) tasks#destroy
EDIT:
To answer a few of the answers: I know that I can use 'as' to name the prefixes, but I think that I shouldn't have to in this case.
From what I understand, 'as' is only used in case you want to change the standard prefix to a personalized one, or if you think your routes might change in the future and you don't want to risk to mess with with your helpers. I should still get correct prefixes without using 'as'.
Here though, the prefix tasks_new with the get method is both linked to the tasks#new and tasks#edit methods, which I don't think is right.
Rails will let you name the routes. For example:
get 'tasks/new', to: 'tasks#new', as: :new_task
Use as for naming the route.
Write the code as following:
Rails.application.routes.draw do
root 'tasks#index'
get 'tasks' => 'tasks#index', as: 'task_list'
get 'tasks/:id' => 'tasks#show', as: 'display_task'
get 'tasks/new' => 'tasks#new', as: 'task_new'
post 'tasks' => 'tasks#create', as: 'task_create
get 'tasks/:id/edit' => 'tasks#edit', as: 'task_edit'
patch 'tasks/:id', to: 'tasks#update'
delete 'tasks/:id', to: 'tasks#destroy'
end
=> After deploying my rails app on Heroku
=> I cannot access to my admin at mysite.heroku.com/admin
--
I run heroku logs
The error : Invalid route name, already in use: 'admin_root'
/app/vendor/bundle/ruby/2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/routing/route_set.rb:549:in `add_route': Invalid route name, already in use: 'admin_root' (ArgumentError)
And You may have defined two routes with the same name using the :as option
2016-10-24T13:43:56.930010+00:00 app[web.1]: You may have defined two routes with the same name using the `:as` option, or you may be overriding a route already defined by a resource with the same naming. For the latter, you can restrict the routes created with `resources` as explained here:
--
This is my config/routes.rb :
Rails.application.routes.draw do
root 'pages#index'
get '/agence' => 'pages#agence'
get '/methode' => 'pages#methode'
get 'projets' => 'projet#index'
get 'projets/:slug' => 'projet#show', as: 'projet'
get '/article' => 'article#index'
get '/article/:slug' => 'article#show', as: 'articles'
get '/contact' => 'pages#contact'
get '/mentionslegales' => 'pages#mentionslegales'
namespace :admin do
resources :projets
resources :articles
resources :users
# get '/' => 'projets#index'
root to: "projets#index"
end
if defined?(DashboardManifest)
namespace :admin do
DashboardManifest::DASHBOARDS.each do |dashboard_resource|
resources dashboard_resource
end
root controller: DashboardManifest::ROOT_DASHBOARD, action: :index
end
end
--
Rake routes :
Prefix Verb URI Pattern Controller#Action
root GET / pages#index
agence GET /agence(.:format) pages#agence
methode GET /methode(.:format) pages#methode
projets GET /projets(.:format) projet#index
projet GET /projets/:slug(.:format) projet#show
article GET /article(.:format) article#index
articles GET /article/:slug(.:format) article#show
contact GET /contact(.:format) pages#contact
mentionslegales GET /mentionslegales(.:format) pages#mentionslegales
admin_projets GET /admin/projets(.:format) admin/projets#index
POST /admin/projets(.:format) admin/projets#create
new_admin_projet GET /admin/projets/new(.:format) admin/projets#new
edit_admin_projet GET /admin/projets/:id/edit(.:format) admin/projets#edit
admin_projet GET /admin/projets/:id(.:format) admin/projets#show
PATCH /admin/projets/:id(.:format) admin/projets#update
PUT /admin/projets/:id(.:format) admin/projets#update
DELETE /admin/projets/:id(.:format) admin/projets#destroy
admin_articles GET /admin/articles(.:format) admin/articles#index
POST /admin/articles(.:format) admin/articles#create
new_admin_article GET /admin/articles/new(.:format) admin/articles#new
edit_admin_article GET /admin/articles/:id/edit(.:format) admin/articles#edit
admin_article GET /admin/articles/:id(.:format) admin/articles#show
PATCH /admin/articles/:id(.:format) admin/articles#update
PUT /admin/articles/:id(.:format) admin/articles#update
DELETE /admin/articles/:id(.:format) admin/articles#destroy
admin_users GET /admin/users(.:format) admin/users#index
POST /admin/users(.:format) admin/users#create
new_admin_user GET /admin/users/new(.:format) admin/users#new
edit_admin_user GET /admin/users/:id/edit(.:format) admin/users#edit
admin_user GET /admin/users/:id(.:format) admin/users#show
PATCH /admin/users/:id(.:format) admin/users#update
PUT /admin/users/:id(.:format) admin/users#update
DELETE /admin/users/:id(.:format) admin/users#destroy
admin_root GET /admin(.:format) admin/projets#index
I don't understand this error and how to resolve it.
I already read all posts about this error but can't find the solution..
Finally the solution was to with draw theses lines from routes.rb :
if defined?(DashboardManifest)
namespace :admin do
DashboardManifest::DASHBOARDS.each do |dashboard_resource|
resources dashboard_resource
end
root controller: DashboardManifest::ROOT_DASHBOARD, action: :index
end
end
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
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)