在Ruby on Rails视图中输出对象属性

时间:2017-06-13 22:27:58

标签: ruby-on-rails ruby ruby-on-rails-5

我正在创建一个简单的应用程序来管理Rails中乐队和音乐家的数据库,而且我在视图文件中打印对象属性值时遇到了问题。

模型是Band,Musician,它们通过连接表连接以建立HABTM关系。这是我的架构文件:

ActiveRecord::Schema.define(version: 20170613125004) do

  create_table "band_musicians", force: :cascade do |t|
    t.integer "band_id"
    t.integer "musician_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["band_id"], name: "index_band_musicians_on_band_id"
    t.index ["musician_id"], name: "index_band_musicians_on_musician_id"
  end

  create_table "bands", force: :cascade do |t|
    t.string "name"
    t.string "description"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "musicians", force: :cascade do |t|
    t.string "name"
    t.string "instrument"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end
end

然后我使用以下seeds.rb脚本插入了一些种子数据,这似乎工作正常。

band_data = [[name:"band 1", description: "band 1 description"],
             [name:"band 2", description: "band 2 description"],
             [name:"band 3", description: "band 3 description"],
             [name:"band 4", description: "band 4 description"]]

musician_data = [[name:"musician 1", instrument:"instrument 1"],
                 [name:"musician 2", instrument:"instrument 2"],
                 [name:"musician 3", instrument:"instrument 3"],
                 [name:"musician 4", instrument:"instrument 4"],
                 [name:"musician 5", instrument:"instrument 1"],
                 [name:"musician 6", instrument:"instrument 5"],
                 [name:"musician 7", instrument:"instrument 1"],
                 [name:"musician 8", instrument:"instrument 2"],
                 [name:"musician 9", instrument:"instrument 3"],
                 [name:"musician 10", instrument:"instrument 1"],
                 [name:"musician 11", instrument:"instrument 4"],
                 [name:"musician 12", instrument:"instrument 1"],
                 [name:"musician 13", instrument:"instrument 2"]]

band_data.each{|name, desc| Band.create(name:name, description:desc)}
musician_data.each{|name, instr| Musician.create(name:name, instrument:instr)}

bands = Band.all()
musicians = Musician.all()

bands.each do |b|
  r = Random.new
  bandSize = r.rand(2..5)
  bandSize.times do
    m = r.rand(0...musicians.length)
    b.band_musicians.create(musician_id: musicians[m].id)
  end
end

然后我想将数据输出到视图,所以我创建了一个Band控制器:

class BandsController < ApplicationController
  def index
    @band_data = Band.includes(:musicians).all
    @musician_data = Musician.all
  end

这个index.html.erb乐队视图:

<% @band_data.each do |b| %>
    <p>
      <%= b.name %>
      <br>
      <%= b.description %>
    <ul>
      <% b.musicians.each do |m| %>
          <li>
            <%= m.name %>
          </li>
      <% end %>
    </ul>
    </p>
    <hr>
<% end %>

数据库似乎已正确播种,但正在输出每个对象的哈希表示而不是属性值。 这就是我得到的:

{:name=>"band 1", :description=>"band 1 description"} 
{:name=>"musician 12", :instrument=>"instrument 1"} 
{:name=>"musician 1", :instrument=>"instrument 1"} 
{:name=>"musician 10", :instrument=>"instrument 1"} 

当我想要更像的东西时:

band 1
band 1 description
 - musician 12
 - musician 1
 - musician 10

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

因为你在方括号内使用key: value语法,所以Ruby假设你想要一个数组内的哈希。在很多情况下,可以省略大括号,Ruby会将其解析为散列。

此外,您可以将属性数组传递给Model::create,它会一次创建多个行。除种子数据外,通常没那么有用。

Band.create [{name:"band 1", description: "band 1 description"},
             {name:"band 2", description: "band 2 description"},
             {name:"band 3", description: "band 3 description"},
             {name:"band 4", description: "band 4 description"}]

Musician.create [{name:"musician 1", instrument:"instrument 1"},
                 {name:"musician 2", instrument:"instrument 2"},
                 {name:"musician 3", instrument:"instrument 3"},
                 {name:"musician 4", instrument:"instrument 4"},
                 {name:"musician 5", instrument:"instrument 1"},
                 {name:"musician 6", instrument:"instrument 5"},
                 {name:"musician 7", instrument:"instrument 1"},
                 {name:"musician 8", instrument:"instrument 2"},
                 {name:"musician 9", instrument:"instrument 3"},
                 {name:"musician 10", instrument:"instrument 1"},
                 {name:"musician 11", instrument:"instrument 4"},
                 {name:"musician 12", instrument:"instrument 1"},
                 {name:"musician 13", instrument:"instrument 2"}]

所以答案是你实际上正确地渲染了视图,这是种子数据搞砸了。

如果您对某些特定方式的呈现方式感到困惑,一种简单的调试方法是将<% console %>放入视图中,Rails会在执行时为该位置提供交互式控制台,允许您可以直接检查运行时环境。