Rails在索引中显示字符串而不是ID

时间:2016-03-06 22:01:19

标签: ruby-on-rails forms ruby-on-rails-4 nested grouped-collection-select

我有以下表单,用户从活动中心页面上的数据库条目列表中选择一个部门。我正在尝试显示部门字符串(:department)而不是部门ID。我试过@activity_center.department.department,但那不起作用。有什么建议吗?

形式:

<%= 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 %>

1 个答案:

答案 0 :(得分:0)

Per MZaragoza这是一个错字。 password_hash()应该是单数的。