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