Rails _path generation path with non id formatting

On my .rb routes, I have:

resources :aquariums do resources :management_roles resources :graphs resources :animals end get 'aquarium', to: 'aquariums#show', :as => :aquarium 

The reason for the last receipt - I have the concept of "current aquarium" in my application. If, say, current_aquarium is set to 1, then in my "show" action / aquarium, the "controller" gets the same rendering as "/ aquariums / 1" with a code like

 @aquarium_id = params[:id] || current_aquarium.id 

Now, and I guess this is due to this other routing, this code:

 <%= link_to aquarium.name, aquarium %> 

or

 <%= link_to aquarium.name, aquarium_path(aquarium) %> 

Creates paths as follows:

 /aquarium.1 

where I usually expect:

 /aquariums/1 

Right?

Upon request ... here that rake routes | grep aquar rake routes | grep aquar gives: (I assume this is the very last route that confuses things, but I would have thought it would handle them in order. And, just FYI, I originally had this route at the top. would be fixed).

  aquarium_management_roles GET /aquariums/:aquarium_id/management_roles(.:format) management_roles#index POST /aquariums/:aquarium_id/management_roles(.:format) management_roles#create new_aquarium_management_role GET /aquariums/:aquarium_id/management_roles/new(.:format) management_roles#new edit_aquarium_management_role GET /aquariums/:aquarium_id/management_roles/:id/edit(.:format) management_roles#edit aquarium_management_role GET /aquariums/:aquarium_id/management_roles/:id(.:format) management_roles#show PUT /aquariums/:aquarium_id/management_roles/:id(.:format) management_roles#update DELETE /aquariums/:aquarium_id/management_roles/:id(.:format) management_roles#destroy aquarium_graphs GET /aquariums/:aquarium_id/graphs(.:format) graphs#index POST /aquariums/:aquarium_id/graphs(.:format) graphs#create new_aquarium_graph GET /aquariums/:aquarium_id/graphs/new(.:format) graphs#new edit_aquarium_graph GET /aquariums/:aquarium_id/graphs/:id/edit(.:format) graphs#edit aquarium_graph GET /aquariums/:aquarium_id/graphs/:id(.:format) graphs#show PUT /aquariums/:aquarium_id/graphs/:id(.:format) graphs#update DELETE /aquariums/:aquarium_id/graphs/:id(.:format) graphs#destroy aquarium_animals GET /aquariums/:aquarium_id/animals(.:format) animals#index POST /aquariums/:aquarium_id/animals(.:format) animals#create new_aquarium_animal GET /aquariums/:aquarium_id/animals/new(.:format) animals#new edit_aquarium_animal GET /aquariums/:aquarium_id/animals/:id/edit(.:format) animals#edit aquarium_animal GET /aquariums/:aquarium_id/animals/:id(.:format) animals#show PUT /aquariums/:aquarium_id/animals/:id(.:format) animals#update DELETE /aquariums/:aquarium_id/animals/:id(.:format) animals#destroy aquariums GET /aquariums(.:format) aquariums#index POST /aquariums(.:format) aquariums#create new_aquarium GET /aquariums/new(.:format) aquariums#new edit_aquarium GET /aquariums/:id/edit(.:format) aquariums#edit aquarium GET /aquariums/:id(.:format) aquariums#show PUT /aquariums/:id(.:format) aquariums#update DELETE /aquariums/:id(.:format) aquariums#destroy aquarium GET /aquarium(.:format) aquariums#show 

Thanks in advance!

Greg

+4
source share
1 answer

Change this last route:

 get 'aquarium', to: 'aquariums#show', :as => :aquarium 

:

 get 'aquarium', to: 'aquariums#show', :as => :current_aquarium 

The problem is that you need to call the same thing:

 aquarium GET /aquariums/:id(.:format) aquariums#show aquarium GET /aquarium(.:format) aquariums#show 

If you make the change above, then the second route will not match when you make this link to the calls ... because it stands now, the second one matches, and as the route says, your argument is used as the format :.

If you make this change, you may need to configure some things if you intentionally refer to "current_aquarium".

+4
source

All Articles