我需要能够输入一个String值,并让它将id输入到表中的那一行。
我正试图在select
中使用HTML
标记来使用Ruby
。但是我需要它以编程方式更改何时将新行添加到表中我从中提取值。
表本身看起来像这样:
Locations
id location_cd
| 1 | SLC|
| 2 | LAS|
等。
我使用Location类将这些值从此表传递到数组中。这是Location类。
class Location < ActiveRecord::Base
def self.select_options
self.all.sorted.collect{ |q| [q.id, q.location_cd]}
end
end
当我调用select_options方法时,这样做会给我这个多维数组。
[[1, "SLC"], [2, "LAS"]]
这是我在控制器中的内容。
def edit
@pallet = Pallet.find(params[:id])
@locations = Location.all
end
def update
@pallet = Pallet.find(params[:id])
if @pallet.update_attributes(pallet_params)
flash[:notice] = "Pallet Updated Successfully."
redirect_to(:action => 'show', :id => @pallet.id)
else
@pallet_count = Pallet.count
render('edit')
end
end
托盘是我要拉的主要类,我需要将location.id输入到托盘表中。
这是我编辑的内容。
<%= link_to("<< Back to List", {:action => 'index'}, :class => 'back-link') %>
<div class="pallets edit">
<h2>Update Pallet</h2>
<%= form_for(:pallet, :url => {:action => 'update', :id => @pallet.id}) do |f| %>
<%= render(:partial => "form", :locals => {:f => f}) %>
<div class="form-buttons">
<%= submit_tag("Update Pallet") %>
</div>
<% end %>
</div>
这会将其重定向到表单,这就是我在表单中的内容。这就是我遇到的问题所在。
<table summary="Pallets from fields">
<tr>
<th><%= f.label(:destination) %></th>
</tr>
<tr>
<td><%= f.select(:destination, Location.select_options) %></td>
</tr>
</table>
这样做会给我一个id
这样的列表。
1
2
但是我想要这样的location_cd
列表。
SLC
LAS
但是我需要它才能进入SLC,在数据库中它会输入1。
要将其置于multidimensional array
的透视图中,我需要能够在用户端输入[[1, "SLC"]]
的第二个值,并且第一个值(1
)应该是进入了表格。
答案 0 :(得分:0)
我做了很多挖掘,我找到了一些有用的东西。
有一个名为options_for_select()
的导轨附带帮助器。这样做会带来类似的东西
<%= select_tag(:destination, '<option value="1">SLC</option>...') %>
它会使用multidimensional array
由于我已经使用multidimensional array
方法获得select_options
,因此我可以将它们彼此结合使用,如下所示:
<td><%= select_tag(:destination, options_for_select(Location.select_options)) %></td>
这完全符合我的需要,并在选择时将正确的值附加到数据库。
另一种方法是没有options_for_select帮助器,但是需要有一个参数可供选择哪一行。
@pallet = Pallet.find(2)
<td><%= select(:pallet, :destination, Location.select_options) %></td>
这个的优势在于它会自动默认为已存在的内容。这使您在更新不是位置的内容时更容易。