2

我有一个HAS_ONE关系:的Rails 3.1 HAS_ONE嵌套的资源:路由不产生 “全能” 的路径

# supplier.rb 

    has_one :presentation 
... 

# presentation.rb 

    belongs_to :supplier 
... 

与如下因素嵌套的路线为他们:

# routes.rb 

resources :suppliers do 
    resource :presentation 
end 

运行rake routes给出:

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

为什么show_action没有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) 

的工作,但为什么呢? ...当rake routes是在我的shell执行它不会出现......

回答

3

我真的不知道为什么,当你做rake routes它没有显示,但你在你的代码试图做supplier_presentation_path(@supplier)?它应该基于你的路线工作。

+0

它的工作,但** **为什么这样做时,它没有反映'耙routes' – fgdemussy

+0

我不知道,也许是因为它是一样的POST。我在我的一个项目上试过了,它也没有显示出来。 – Robin

0

从来没有它应该为你工作。试试这个:

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

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

你不需要@presentation当使用supplier_presentation_path自供应商has_one演示文稿;) – Robin

+0

@Robin,哦,这是真的。我没有注意到这种关系 – fl00r