Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails _path helper generating path with format not id

In my routes.rb 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 get is I have the notion of "current aquarium" in my app. If, say, current_aquarium is set to 1, then in my controller's 'show' action '/aquarium' gets the same rendering as '/aquariums/1' with code like

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

Now, and I'm assuming this is thanks to this different routing, this code:

<%= link_to aquarium.name, aquarium %>

or

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

Generates paths like this:

/aquarium.1

where I'd typically expect:

/aquariums/1

Right?

Per request... here's what rake routes | grep aquar yields: (I'm assuming it's that very last route that is messing things up, but I would have thought that it would process these in order. And, just FYI, I originally had that route at the top. Moved it to the bottom assuming it would fix).

    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

like image 582
Greg Rogers Avatar asked Jan 05 '13 14:01

Greg Rogers


1 Answers

Change that last route from this:

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

to this:

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

The problem is that you have to routes named the same thing:

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

If you make the change above then that second route will not match when you make those link to calls... as it stands now, the second one is matching and like the route says is using your argument as the :format.

If you do make this change, you may need to tweak some things if you are intentionally linking to 'current_aquarium'.

like image 99
Philip Hallstrom Avatar answered Jan 04 '23 13:01

Philip Hallstrom