Arslan Ali
Arslan Ali

Reputation: 17812

How to use a file of different name while using render @collection in Rails?

Rails saves us an each loop by letting us directly pass the collection variables to render method like following:

<%= render @products %>

By default, it will look for a file named _product.html.erb, and render it for each single item in @products collection. For somehow, the file has been named as _custom_product.html.erb, now how to tell render method to render a file of name other than the standard _product.html.erb?

Upvotes: 0

Views: 236

Answers (1)

Max Filippov
Max Filippov

Reputation: 2092

You can go either way:

To access your products via product local variable in the partial:

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

Or via custom_product in the partial:

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

Upvotes: 1

Related Questions