PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, May 18, 2022

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

 May 18, 2022     partial, render, ruby-on-rails, ruby-on-rails-4     No comments   

Issue

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?


Solution

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 %>


Answered By - Maxim Filippov
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing