vendredi 5 juin 2015

How to define a custom nested path in rails

I have a model named client and another model called client_preference. The relationship between them is one client has many client_preferences. Now I want to create methods for updating and deleting client_preferences. For that I generate the routes as:

map.resources :clients do |client|
  client.resources :client_preferences, only: [:edit, :update, :destroy]
end

and I get the following named routes:

edit_client_client_preference GET    /clients/:client_id/client_preferences/:id/edit(.:format)                     {:controller=>"client_preferences", :action=>"edit"}
client_client_preference      PUT    /clients/:client_id/client_preferences/:id(.:format)                          {:controller=>"client_preferences", :action=>"update"}
                              DELETE /clients/:client_id/client_preferences/:id(.:format)                          {:controller=>"client_preferences", :action=>"destroy"}

Now, I want to customize the routes names from client_client_preference to client_preference and similarly for other routes generated so that the client is word not repeated twice in the path names generated. Is there a way to do that or do I need to manually define the routes? Any help or pointers will be highly helpful.

Aucun commentaire:

Enregistrer un commentaire