2016-03-15 45 views
0

Rails的通過讓我們直接通過收集變量render方法像下面爲我們節省了each環:如何在Rails中使用render @collection時使用不同名稱的文件?

<%= render @products %> 

默認情況下,它會尋找一個名爲_product.html.erb文件,並使其對於@products集合中的每個單個項目。不知何故,該文件已被命名爲_custom_product.html.erb,現在如何告訴render方法來呈現名稱不同於標準的文件_product.html.erb

+1

請問這個'< %=渲染部分:「custom_product」,集合:@products%>'工作? – Pavan

+0

@Pavan不,它不起作用。它說_custom_product.html.erb中的未定義變量'產品' –

回答

1

你可以去任何一種方式:

要通過product局部變量訪問您的產品在部分:

<%= render partial: "custom_product", collection: @products, as: :product %> 

或者通過custom_product在部分:

<%= render partial: "custom_product", collection: @products %> 
+0

它不起作用。 –

+0

@BilalMaqsood我已經更新了答案,檢查出來 –

+0

工程就像一個魅力。 –

相關問題