[RUBY] [Rails] About collection when rendering partial (when loading partial template)

When calling a partial template with rails this time, I learned that it is easier to pass local variables by using collection, so I will output it.

If you don't know about the render method, please search.

collection makes it very easy to send local variables to partial templates

First of all, there are answer model and controller as a premise.

def index
@answers = Answer.all
end

Now each person's answer is in @answer.

Now if you use local to send a variable to the partial template

<% @answers.each do |answer| %>
  <%= render partial: "answer", locals: { answer: answer } %>
<% end %>

It will be.

However, in the above, if @answers contains 10 pieces of information, the render partical will be called once (10 times in total). In other words, it slows down.

Therefore, collection is a simple way to delay variables to partial templates.

Convenient to use collection

<%= render partial: "answer", collection: @tweets %>

With just this, you can send a local variable to a partial template. Even if 10 pieces of information are stored in @answers, they will be passed together in a partial template to create 10 views.

Since I am also in the learning stage, mistakes may occur. We apologize for the inconvenience, but we would appreciate it if you could point out.

Recommended Posts

[Rails] About collection when rendering partial (when loading partial template)
Rails: About partial templates
[Rails basics] About errors when loading screens (NameError, LoadError)
[rails] How to create a partial template
Implement iteration in View by rendering collection [Rails]
About Rails routing
[Rails] About ActiveJob ,!
About Rails controller
About RSpec (Rails)
[Rails] About error resolution when installing devise and activeadmin
About Class loading and initialization when the JVM starts