rails 再次列出我的整个数据库 table,在我的 div 中按计划列出之后

rails lists my entire database table, again, after already been listed as planned in my divs

我的视图在我打算列出它们的面板 div 之外重新列出了我的所有数据库 table 内容。

HTML

<div class="row">
  <div class="small-12 large-12 columns"><h2>All recipes</h2></div>
  <%= @recipes.each do |x| %>
    <div class="small-2 large-4 columns">
      <div draggable="true" class="panel">
        <h2><small><%= x.name %></small></h2>
        <p><%= x.ingredients %></p>
        <p><%= x.how %></p>
        <p><%= x.nutrients %></p>
      </div>
    </div>
  <% end %>
</div>

模特

class CreateRecipes < ActiveRecord::Migration
  def change
    create_table :recipes do |t|
      t.string :name
      t.text :ingredients
      t.text :nutrients
      t.text :how
      t.timestamps
    end
  end
end

控制器

def index
  @recipes = Recipe.all
end

Rubyreturns数组的内容在each的末尾。您可以通过在 Rails 控制台中放置一个循环来查看:

@recipes.each { |r| puts r.name }

你会得到一个名字列表,但你也会在最后看到一系列食谱。

在您的模板中,您的 each 语句是用 <%= @recipes.each do |x| %> 打开的。 ERB 将 = 解释为您希望此语句的输出出现在呈现的模板中。对于 each,您不希望这样。尝试将 = 替换为 -(或者干脆将其删除)。