【问题标题】:Rails 3.1 has_one nested resource: routing not generating "all" pathsRails 3.1 has_one 嵌套资源:路由不生成“所有”路径
【发布时间】:2011-08-11 16:40:19
【问题描述】:

我有一个 has_one 关系:

# supplier.rb

  has_one :presentation
...

# presentation.rb

  belongs_to :supplier
...

以及它们的以下嵌套路由:

# routes.rb

resources :suppliers do
  resource :presentation
end

运行rake routesgives:

    supplier_presentation POST ... {:action=>"create", :controller=>"presentations"}
 new_supplier_presentation GET ... {:action=>"new", :controller=>"presentations"}
edit_supplier_presentation GET ... {:action=>"edit", :controller=>"presentations"}
                           GET ... {:action=>"show", :controller=>"presentations"}
                           PUT ... {:action=>"update", :controller=>"presentations"}
                        DELETE ... {:action=>"destroy", :controller=>"presentations"}

为什么没有 name_helper 用于显示操作?

我可以通过以下方式解决问题:

resources :suppliers do
  resource :presentation, :except => :show do
    get "" => "presentations#show", as: "presentation"
  end
end

给出路​​线:

presentation_supplier_presentation GET ... {:controller=>"presentations", :action=>"show"}

但我们现在都不是正确的处理方式..

有什么建议吗?

--

(已编辑)

supplier_presentation_path(@supplier)

确实有效,但是为什么?...在我的shell上执行rake routes时它不会出现...

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 routing nested-resources


    【解决方案1】:

    我真的不知道为什么当您执行rake routes 时它没有显示,但是您是否尝试在您的代码中执行supplier_presentation_path(@supplier)?它应该根据您的路线工作。

    【讨论】:

    • 确实有效,但是为什么在做rake routes时没有反映出来
    • 我不知道,可能是因为它与 POST 相同。我在我的一个项目上试过了,也没有显示。
    【解决方案2】:

    它应该为你工作的越少越好。试试这个:

    link_to "Presentation", [@suplier, @presentation]
    

    link_to "Presentation", suplier_presentation_path(@suplier, @presentation)
    

    【讨论】:

    • 使用supplier_presentation_path 时不需要@presentation,因为supplier has_one 演示;)
    • @Robin,哦,那是真的。我没有注意到这种关系
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-11
    • 2016-04-05
    • 2011-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-13
    相关资源
    最近更新 更多