模式:Rails的,的has_many,belongs_to的
create_table "reports", :force => true do |t|
t.integer "user_id"
t.string "apparatus"
t.string "capt"
t.text "body"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", :force => true do |t|
t.string "login", :limit => 40
t.string "name", :limit => 100, :default => ""
t.string "email", :limit => 100
t.string "crypted_password", :limit => 40
t.string "salt", :limit => 40
t.datetime "created_at"
t.datetime "updated_at"
t.string "remember_token", :limit => 40
t.datetime "remember_token_expires_at"
t.string "rank"
t.integer "shift"
t.integer "access"
end
用戶模型:
Class User < ActiveRecord::Base
has_many :reports
# bunch of other stuff thats not important
end
報表模型:
Class Report < ActiveRecord::Base
belongs_to :user
end
的意見/報告/指數
<% @reports.each do |report| %>
<tr>
<td><%= report.user_id %></td> # ****THIS IS THE LINE IN QUESTION****
<td><%= report.apparatus %></td>
<td><%= report.capt %></td>
<td><%= report.body %></td>
<td><%= link_to 'Show', report %></td>
<td><%= link_to 'Edit', edit_report_path(report) %></td>
<td><%= link_to 'Destroy', report, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
我希望能夠顯示創建報告的用戶的名稱。我假設聲明belongs_to和has_many關聯可以通過編寫report.user.name或類似的東西來實現。我哪裏錯了?
OK,問題是我在我的表中的一個元組是沒有一個用戶ID。所以它試圖在nil上找到名稱方法。我是一個屁股。 –