0

我有两个模型命名:um_org_data.rb

class UmOrgDatum < ActiveRecord::Base


  attr_accessible :org_description, :org_name, :webdomain, :addresses, :addresses_attributes

  has_many :addresses

  accepts_nested_attributes_for :addresses
end

addresses.rb

class Address < ActiveRecord::Base
  attr_accessible :offc_addr

  belongs_to :um_org_datum

end

在我的控制器中,我使用 join.. 如下代码um_org_data.rb

class UmOrgDataController < ApplicationController
  # GET /um_org_data
  # GET /um_org_data.json
  def index
     @um_org_data = UmOrgDatum.joins(:addresses)
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @um_org_data }
    end
  end

现在在我看来,我想从我使用 join 的控制器addresses.rb的视图中显示 offc_addr 列的数据um_org_data.html.erb。我的视图代码如下:

<p id="notice"><%= notice %></p>

<div class="container">
  <div class="row">
                    <div class="span3 pull-right">
                      <div class="well">
                        <h2>Heading</h2>
                        <p>Sample text</p>
                      </div> 
                    </div> 

    <div class="span9">
            <h2> List of Organization Details</h2>
            <%= b_search_bar(um_org_data_path).html_safe %><br/><br/>




                    <table class="table table-hover table-condensed">
                      <tr>
                        <th>Organization Name</th>
                        <th>Organization Description</th>
                        <th>Web Domain</th>
                        <th>Address</th>
                        <th>Actions</th>
                      </tr>

                      <% @um_org_data.each do |um_org_data| %>
                        <tr>
                            <td><%= um_org_data.org_name %></td>
                            <td><%= um_org_data.org_description%></td>
                            <td><%= um_org_data.webdomain%></td>
                            <td><%= um_org_datum.addresses.offc_addr%></td>
                            <td>
                              <%= link_to "<i class='icon-eye-open'></i>".html_safe, um_org_data,"data-original-title" => "View Details", "data-placement" => "bottom", :rel => "nofollow", :class => 'bg-color-none' %>

                              <%= link_to "<i class= 'icon-edit'></i>".html_safe, edit_um_org_datum_path(um_org_data), "data-original-title" => "Edit", "data-placement" => "bottom", :rel => "nofollow", :class => 'bg-color-none'%>

                              <%= link_to "<i class= 'icon-trash'></i>".html_safe, um_org_data, method: :delete, data: { confirm: 'Are you sure?' }, "data-original-title" => "Delete", "data-placement" => "bottom", :rel => "nofollow", :class => 'bg-color-none' %>   
                            </td>
                        </tr>

                      <%end%>
                    </table>
        <div><a class="btn" href="/um_org_data/new" style="text-color:black">Add Organization</a>
        </div>
    </div>
  </div>
</div>

告诉我如何在该视图中显示其他表的列。

4

1 回答 1

1

您是否希望每个组织有多个地址?如果是,那么做

<td><%= um_org_datum.addresses.pluck(:offc_addr).join(', ') %></td>

这将为您提供逗号分隔列表中的一列地址。如果每个组织应该只有一个地址,has_one :address在您看来,使用和,

<td><%= um_org_datum.address.offc_addr %></td>

您当前遇到的问题是您将其视为um_org_datum.addresses单个模型实例,而实际上它是一个地址数组。

于 2013-08-27T06:14:42.600 回答