【发布时间】:2014-06-02 15:25:40
【问题描述】:
我有一张桌子,想设置默认值
这是表格:
|customers|
|id| |name|
1 ABC
2 DEF
3 GHI
4 JKL
这里是控制器:
@customers.Customer.all
@customer_selected = Customer.find(:all,:conditions=>['id=1'])
这是视图,显示所有客户
<%= select_tag "customer_id", options_for_select(@customers.collect {|t| [t.name,t.id]},params[:customer_id].to_i) %>
我尝试这一行选择 customer_id=1 作为默认值(选中)
<%= select_tag "customer_id", options_for_select(@customers.collect {|t| [t.name,t.id]},params[:customer_id].to_i),:selected =>@customer_selected.name %>
我收到了这个错误
undefined method `name' for #<Array:0x7f31d8098560>
请有人可以帮助我吗?
【问题讨论】:
-
您指向数组中的一个元素。由于数组是元素的集合,因此您需要指定要访问的元素(在本例中为数组的第一个元素)或在数组中迭代(each、for 等)。 t[1].name, t[1].id 应该可以工作。
-
你的控制器代码应该写成
@customers = Customer.all吗?这可能是您的问题,而不是选择。
标签: ruby-on-rails ruby ruby-on-rails-2