的名字在我的Rails應用程序我有設備端口的列表視圖和每個設備端口所屬的設備:如何獲得的外鍵
device.rb
has_many :device_ports, :foreign_key => "device_id"
device_port。 RB
belongs_to :device
device_port_controller.rb
def list
@device_ports = DevicePort.paginate(:all, :page => params[:page], :per_page => 100, :order => "name")
@devices = Device.find(:all)
end
list.rhtml
<table width="100%">
<tr>
<th>Name</th>
<th>Device</th>
</tr>
<%= render :partial => 'device_port/list_item', :collection => @device_ports %>
</table>
_ list_item.rhtml
<tr>
<td><a href='/device_port/update/<%= list_item.id %>'><%= list_item.name %></a></td>
<td><%= list_item.device_id %></td>
</tr>
所以我從device_ports
表顯示device_id
但其實我是想顯示name
表1中的devices
列nstead。
我做了一些非常相似的事情,可能在我的應用程序的其他地方稍微有些困難,我有一個預選的下拉列表,並選擇了正確的設備名稱,但我似乎無法弄清楚如何使用select菜單。
我在上面的控制器中的@devices
變量的設備,並試圖用list_item.device_id
來查看,但它似乎沒有工作。
這可能是相當直接的事情,但我似乎無法找到獲得理想結果的正確方法。
as device_ports belongs_to device so'list_item.device.name'謝謝! –