2012-01-06 17 views
0

什麼是約定使用部分列出的項目,VS部分節目顯示/編輯/新表單部分與列表部分的Rails約定?

例如在概述一會想做的事: [應用程序/零件/] (件/ index.html.erb)

<h1> Parts and stuff! </h1> 
<table> 
# headers and such 
<%= render :partial => @parts %> 
</table> 
# calls _part to make a list, usually the 3 most important things 

然後我們有一些像 [應用程序/零件/新] (件/ new.html.erb)

# what is the convention for the partial here? 
# Said partial would be used for new/edit 

我知道你可以指定render:partial =>「path/to/partial」,但是我希望基於資源的路由通過一些常規的名字來獲得一個部分。

回答

0

通常_form_part或_part根據上下文。

1

Rails實際上並沒有部分名稱的約定。約定適用於控制器操作的RESTful模板名稱,即索引,新建,顯示,編輯。動作創建,更新和銷燬使用重定向,而不是直接渲染任何東西。

部分有兩個目的:一是當你想避免重複,即新/編輯形式。另一種是當你想用AJAX更新頁面時。命名約定基本上取決於你,只是試圖保持一致。

但是路線(基於資源或其他方式)並不真正適用於部分;它們由模板引擎處理,而不是調度程序。

如果您希望看到有效使用部分元件的發電機,請嘗試Ryan Bates'nifty_generators gem。

你可以在這裏找到:https://github.com/ryanb/nifty-generators

+0

我不同意。最明顯的相反點是_partial.html.foo。獲得rails來渲染一個不以'_'開頭的部分是相當多的額外工作。同樣,你可以使用諸如render的shorthands:partial => object.has_many_relations。它會查找「_has_many_relation.html」 – EnabrenTane 2012-01-23 16:36:18