简体   繁体   中英

Iterating over an object in Rails template

controller:

  def show
    @recipe = Recipe.find(params[:id])
    @ingredients = @recipe.ingredients
    @steps = @recipe.steps
  end

template:

  <%= @steps.each do |step| %>
    <li>
      <%= step.sequence %>
      <%= step.description %>
    </li>
  <% end %>

The array of objects passed to my template is being iterated over and then is showing the actual array of fields at the end (below step 5 in the picture). I want to display properties of each object, not the actual list of objects.

在此处输入图片说明

What am I doing wrong?

You are doing <%= @steps.each do |step| %>...<% end %> <%= @steps.each do |step| %>...<% end %> , which prints the result of evaluating each , which is the receiver @steps .

To avoid printing it, change that to <% @steps.each do |step| %>...<% end %> <% @steps.each do |step| %>...<% end %> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM