0
我有以下表单,用户从活动中心页面上的数据库条目列表中选择一个部门。我试图显示部门字符串(:department
)而不是部门ID。我试过@activity_center.department.department
但这不起作用。有什么建议么?Rails在索引中显示字符串不是ID
形式:
<%= f.select :department_id, options_from_collection_for_select(@departments, 'id', 'department'), hide_label: true, :multiple => false %>
系车型:
class Department < ActiveRecord::Base
validates :department, :presence => true
has_many :activity_centers
end
活动中心型号:
class ActivityCenter < ActiveRecord::Base
validates :activity_center, :presence => true
validates :department_id, :presence => true
belongs_to :departments
end
活动中心索引页:
<% @activity_centers.each do |activity_center| %>
<tr>
<td><%= activity_center.activity_center %></td>
<td><%= activity_center.department.department %></td>
<td style="text-align:right;">
<%= link_to 'View Activity Center', activity_center_path(activity_center), class: "btn btn-success btn-xs" %>
<%= link_to 'Edit', edit_activity_center_path(activity_center), class: "btn btn-default btn-xs" %>
<%= link_to 'Delete', activity_center_path(activity_center), class: "btn btn-danger btn-xs",
method: :delete,
data: { confirm: 'Confirm you want to delete this activity_center.' } %></td>
</tr>
<% end %>
我的第一个问题是在“活动中心模型:”它是“belongs_to:部门”或“belongs_to:部门”我认为你有它单数 – MZaragoza
是的!非常感谢你的支持。 –