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)
Related
=> 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 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
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
I have a mystic problem....
In my routes.rb I have some routes defined and for exemple
resources :projects, :except => [:destroy] do
get :edit_flyer, :on => :member
get :guests, :on => :member
end
If I run a rake routes, I get
edit_flyer_project GET /projects/:id/edit_flyer(.:format) {:controller=>"projects", :action=>"edit_flyer"}
guests_project GET /projects/:id/guests(.:format) {:controller=>"projects", :action=>"guests"}
GET /projects(.:format) {:controller=>"projects", :action=>"index"}
projects POST /projects(.:format) {:controller=>"projects", :action=>"create"}
new_project GET /projects/new(.:format) {:controller=>"projects", :action=>"new"}
GET /projects/:id(.:format) {:controller=>"projects", :action=>"show"}
project PUT /projects/:id(.:format) {:controller=>"projects", :action=>"update"}
edit_project GET /projects/:id/edit(.:format) {:controller=>"projects", :action=>"edit"}
As you can see, the show action is defined.
But in my rails applications, the route show is not defined.
I add this in my application controller just to monitored the routes.
before_filter :zba
def zba
ActionController::Routing::Routes.named_routes.routes.each do |name, route|
puts "%20s: %s" % [name, route]
end
exit
end
And it appears that the route action is not defined ....
Then, I tryed to clean my routes.rb, like removing all my back namespace, and magically it works.
It seems to be a bug, or I don't know what appened.
Have you any idea how to debug this ? I also tryed to remove plugin/gems. No change.
I run with Rails3.rc with ruby 1.8.7 !
Thanks for your help !
Try this
resources :projects, :except => [:destroy] do
member do
get :edit_flyer
get :guests
end
end
I have a primary model for my project, Place.rb with a places_controller, and I've currently got it exactly the way I want for the users end of my project. With a nested photos controller, review, etc.
I want now to create a management resource which is mostly just an alias for Places, with its own nested resources, some of them overlapping, some of them new.
I tried creating a new controller for it called Manage, but I'm having a hard time with routes. I'm not quite sure the hangup is, but I figure I'm doing something very wrong. I had little difficulty when I was using Places as controller to a real model and nesting other resources below it.
But for example trying to create a new record for a nested resource doesn't route correctly.
I can get a route path like new_manage_room_path(#place) for a link_to to work fine. But
for creating a New announcement in a form:
form_for manage_room_path(#place) doesn't work correctly given a valid id. I've tried many other combinations supplying the object and :url.
Should I avoid using a separate controller and just create an alias or what is the special routing for this purpose?
map.resources :manage, :collection => { :upcoming => [ :post, :get ], :pending => [ :post, :get ] } do |manage|
manage.resources :rooms
manage.resources :room_rates, :as => :rates
manage.resources :availables
manage.resources :manage_bookings, :as => :bookings
end
map.resources :places do |place|
place.resources :bookings
place.resources :photos, :collection => { :sort => :post }
place.resources :reviews, :only => [ :index, :show ]
end
manage_rooms GET /manage/:manage_id/rooms(.:format) {:controller=>"rooms", :action=>"index"}
POST /manage/:manage_id/rooms(.:format) {:controller=>"rooms", :action=>"create"}
new_manage_room GET /manage/:manage_id/rooms/new(.:format) {:controller=>"rooms", :action=>"new"}
edit_manage_room GET /manage/:manage_id/rooms/:id/edit(.:format) {:controller=>"rooms", :action=>"edit"}
manage_room GET /manage/:manage_id/rooms/:id(.:format) {:controller=>"rooms", :action=>"show"}
PUT /manage/:manage_id/rooms/:id(.:format) {:controller=>"rooms", :action=>"update"}
DELETE /manage/:manage_id/rooms/:id(.:format) {:controller=>"rooms", :action=>"destroy"}
manage_room_rates GET /manage/:manage_id/rates(.:format) {:controller=>"room_rates", :action=>"index"}
POST /manage/:manage_id/rates(.:format) {:controller=>"room_rates", :action=>"create"}
new_manage_room_rate GET /manage/:manage_id/rates/new(.:format) {:controller=>"room_rates", :action=>"new"}
edit_manage_room_rate GET /manage/:manage_id/rates/:id/edit(.:format) {:controller=>"room_rates", :action=>"edit"}
manage_room_rate GET /manage/:manage_id/rates/:id(.:format) {:controller=>"room_rates", :action=>"show"}
PUT /manage/:manage_id/rates/:id(.:format) {:controller=>"room_rates", :action=>"update"}
DELETE /manage/:manage_id/rates/:id(.:format) {:controller=>"room_rates", :action=>"destroy"}
manage_availables GET /manage/:manage_id/availables(.:format) {:controller=>"availables", :action=>"index"}
POST /manage/:manage_id/availables(.:format) {:controller=>"availables", :action=>"create"}
new_manage_available GET /manage/:manage_id/availables/new(.:format) {:controller=>"availables", :action=>"new"}
edit_manage_available GET /manage/:manage_id/availables/:id/edit(.:format) {:controller=>"availables", :action=>"edit"}
manage_available GET /manage/:manage_id/availables/:id(.:format) {:controller=>"availables", :action=>"show"}
PUT /manage/:manage_id/availables/:id(.:format) {:controller=>"availables", :action=>"update"}
DELETE /manage/:manage_id/availables/:id(.:format) {:controller=>"availables", :action=>"destroy"}
manage_manage_bookings GET /manage/:manage_id/bookings(.:format) {:controller=>"manage_bookings", :action=>"index"}
POST /manage/:manage_id/bookings(.:format) {:controller=>"manage_bookings", :action=>"create"}
new_manage_manage_booking GET /manage/:manage_id/bookings/new(.:format) {:controller=>"manage_bookings", :action=>"new"}
edit_manage_manage_booking GET /manage/:manage_id/bookings/:id/edit(.:format) {:controller=>"manage_bookings", :action=>"edit"}
manage_manage_booking GET /manage/:manage_id/bookings/:id(.:format) {:controller=>"manage_bookings", :action=>"show"}
PUT /manage/:manage_id/bookings/:id(.:format) {:controller=>"manage_bookings", :action=>"update"}
DELETE /manage/:manage_id/bookings/:id(.:format) {:controller=>"manage_bookings", :action=>"destroy"}
pending_manage POST /manage/pending(.:format) {:controller=>"manage", :action=>"pending"}
GET /manage/pending(.:format) {:controller=>"manage", :action=>"pending"}
upcoming_manage POST /manage/upcoming(.:format) {:controller=>"manage", :action=>"upcoming"}
GET /manage/upcoming(.:format) {:controller=>"manage", :action=>"upcoming"}
manage_index GET /manage(.:format) {:controller=>"manage", :action=>"index"}
POST /manage(.:format) {:controller=>"manage", :action=>"create"}
new_manage GET /manage/new(.:format) {:controller=>"manage", :action=>"new"}
edit_manage GET /manage/:id/edit(.:format) {:controller=>"manage", :action=>"edit"}
manage GET /manage/:id(.:format) {:controller=>"manage", :action=>"show"}
PUT /manage/:id(.:format) {:controller=>"manage", :action=>"update"}
DELETE /manage/:id(.:format) {:controller=>"manage", :action=>"destroy"}
Try:
<% form_for #new_room, :url => manage_rooms_path(#place) do |f| %>
or maybe it will work this way:
<% form_for manage_rooms_path(#place, #new_room) do |f| %>
#new_room is new instance of Room model, so in controller add:
#new_room = Room.new