как использовать имена действий в маршрутизации rails3 с match - PullRequest
0 голосов
/ 15 сентября 2010

У меня есть путь http://localhost:3000/recipes/1/ingredient/3

В моих маршрутах я определяю

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

Однако я не думаю, что мне следует определять каждое действие в моем файле маршрутов, у меня не должно быть отдельной записи для «ингредиентов # шоу», чем другой для «ингредиентов # редактирования».

Как мне определить соответствие, чтобы оно принимало либо значение по умолчанию, либо использовало действие, определенное в link_to.

В настоящее время мой link_to определен как

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

или

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

1 Ответ

3 голосов
/ 15 сентября 2010

Я думаю, что вы ищете ресурсы вместо совпадения ...

resources :recipes do
  resources :ingredients
end

дает вам:

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"}

поэтому ваше действие редактирования становится:

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

и действие шоу становится:

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

В противном случае вам придется сделать что-то вроде:

<%= link_to 'Edit Ingredient', :controller => :ingredients, :action => :show, :id => @ingredient.id, :recipe_id => @ingredient.recipe %>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...