【问题标题】:how to use action names in rails3 routing with match如何在rails3路由中使用匹配的动作名称
【发布时间】:2010-09-14 22:45:56
【问题描述】:

我有一条路 http://localhost:3000/recipes/1/ingredient/3

在我的路线中,我定义了

匹配 "/recipes/:recipe_id/ingredients/:id" => "ingredients#show"

但是,我认为我不应该在路由文件中定义每个操作,我不应该为“ingredients#show”设置一个单独的条目,而不是为“ingredients#edit”设置另一个条目。

如何定义我的匹配,以便它采用默认值或使用在 link_to 中定义的操作。

目前,我的 link_to 定义为

 :edit,
                                  :控制器 => :成分 %>

 :show,
                                  :控制器 => :成分 %>

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    我认为您正在寻找资源而不是匹配...

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

    【讨论】:

    • 感谢 jenjenut。奇怪的是,我总是有资源条目,但我的 link_to 写错了。如果你的答案没有那么彻底,它可能只会让我更深入地了解兔子!
    猜你喜欢
    • 2020-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-30
    • 1970-01-01
    • 2011-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多