簡體   English   中英

如何在具有匹配項的rails3路由中使用動作名稱

[英]how to use action names in rails3 routing with match

我有一個http:// localhost:3000 / recipes / 1 / ingredient / 3的路徑

在路線中,我定義

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

但是,我不認為應該在我的路線文件中定義每個動作,對於“ ingredients#show”,我應該沒有一個單獨的條目,而對於“ ingredients#edit”,則沒有另一個條目。

如何定義匹配項,使其采用默認值或使用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 %>

我認為您正在尋找資源而不是比賽...

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 %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM