pedalpete
pedalpete

Reputation: 21536

how to use action names in rails3 routing with match

I've got a path of http://localhost:3000/recipes/1/ingredient/3

In my routes, I define

match  "/recipes/:recipe_id/ingredients/:id" => "ingredients#show"

However, I don't think I should be defining each action in my routes file, I shouldn't have a seperate entry for 'ingredients#show' than another for 'ingredients#edit'.

How do I define my match so that it takes either a default or uses the action defined in the link_to.

Currently, my link_to is defined as

<%= link_to 'Edit Ingredient', [@ingredient.recipe, @ingredient],
                                  :method => :edit,
                                  :controller => :ingredients %>

or

<%= link_to 'Edit Ingredient', [@ingredient.recipe, @ingredient],
                                  :method => :show,
                                  :controller => :ingredients %>

Upvotes: 0

Views: 198

Answers (1)

jenjenut233
jenjenut233

Reputation: 1938

I would think you are looking for resources instead of match...

resources :recipes do
  resources :ingredients
end

gives you:

recipe_ingredients GET    /recipes/:recipe_id/ingredients(.:format)          {:action=>"index", :controller=>"ingredients"}
    recipe_ingredients ingredient   /recipes/:recipe_id/ingredients(.:format)          {:action=>"create", :controller=>"ingredients"}
 new_recipe_ingredient GET    /recipes/:recipe_id/ingredients/new(.:format)      {:action=>"new", :controller=>"ingredients"}
edit_recipe_ingredient GET    /recipes/:recipe_id/ingredients/:id/edit(.:format) {:action=>"edit", :controller=>"ingredients"}
     recipe_ingredient GET    /recipes/:recipe_id/ingredients/:id(.:format)      {:action=>"show", :controller=>"ingredients"}
     recipe_ingredient PUT    /recipes/:recipe_id/ingredients/:id(.:format)      {:action=>"update", :controller=>"ingredients"}
     recipe_ingredient DELETE /recipes/:recipe_id/ingredients/:id(.:format)      {:action=>"destroy", :controller=>"ingredients"}
         recipes GET    /recipes(.:format)                           {:action=>"index", :controller=>"recipes"}
         recipes ingredient   /recipes(.:format)                           {:action=>"create", :controller=>"recipes"}
      new_recipe GET    /recipes/new(.:format)                       {:action=>"new", :controller=>"recipes"}
     edit_recipe GET    /recipes/:id/edit(.:format)                  {:action=>"edit", :controller=>"recipes"}
          recipe GET    /recipes/:id(.:format)                       {:action=>"show", :controller=>"recipes"}
          recipe PUT    /recipes/:id(.:format)                       {:action=>"update", :controller=>"recipes"}
          recipe DELETE /recipes/:id(.:format)                       {:action=>"destroy", :controller=>"recipes"}

so your edit action becomes:

<%= link_to 'Edit Ingredient', edit_recipe_ingredient_path(@ingredient.recipe, @ingredient) %>

and show action becomes:

<%= link_to 'Edit Ingredient', [@ingredient.recipe, @ingredient] %>

Otherwise, you will have to do something like:

<%= link_to 'Edit Ingredient', :controller => :ingredients, :action => :show, :id => @ingredient.id, :recipe_id => @ingredient.recipe %>

Upvotes: 3

Related Questions