2011-09-22 70 views
0

这是我的形式:与表单时遇到下拉列表中的Ruby on Rails的

<table> 
    <tbody> 
    <% form_for :HhActiveCarrier, @carriers, :url => { :action => "update" } do |f| %> 
    <% for carrier in @carriers %> 
    <tr> 
     <%= render :partial => "summary_detail", :locals => {:carrier => carrier, :f => f} %> 
    </tr> 
    <% end %> 
    </tbody> 
</table> 
    <%= submit_tag "Update" %> 
    <% end %> 

随着我的部分:

<td class="tn"><%= h(carrier.name.to_s()) -%></td> 
<td class="sc"><%= h(carrier.country.to_s()) -%></td> 
<td class="sc"><%= select_tag(:country, options_for_select(@countries)) -%></td> 

这是控制器,我定义变量:

class ActiveCarriersController < ApplicationController 

    def index 
     @carriers = HhActiveCarrier.find(:all) 
     for carrier in @carriers 
      country = carrier["country"] 
      if country.nil? 
       carrier["country"] = "none" 
      end 
     end 
     @countries = ["USA", "UK", "Canada"] 
    end 

所有这些工作。但是,如果我做的形式改变下拉列表这样的:

<td class="sc"><%= f.select("country", @countries) -%></td> 

我得到这个错误:

Showing app/views/active_carriers/_summary_detail.rhtml where line #3 raised: 

undefined method `country' for #<Array:0xef79a08> 

Extracted source (around line #3): 

1: <td class="tn"><%= h(carrier.name.to_s()) -%></td> 
2: <td class="sc"><%= h(carrier.country.to_s()) -%></td> 
3: <td class="sc"><%= f.select("country", @countries) -%></td> 

Trace of template inclusion: /app/views/active_carriers/_summary.rhtml, >/app/views/active_carriers/index.rhtml 

我在做什么毛病我的形式选择?我正在使用Ruby on Rails 2.3.8

Stackoverflow告诉我,我没有太多的解释我所有的代码,所以我只是写东西。我真的不知道还有什么可以解释的,所以如果你不了解所有事情,就问问题。此外,我不能引用错误消息,因为Stackoverflow一直告诉我我的代码没有代码块,所以我必须在错误消息的周围放置代码块。

+0

我认为你需要调用options_for_select(@countries)到数组转换成可选择的选项。 http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-options_for_select – lashleigh

+0

我将它改为:<%= f.select(:country,options_for_select (@countries)) - %>,我仍然得到#的“undefined method country”错误 –

回答

0

试试这个:f.select("carrier", "country", options_for_select(@countries))

+0

所以我试过了:f.select(“carrier”,“country”,options_for_select(@countries ))。我得到这个错误:未定义的方法'合并'为#。阅读后:http://stackoverflow.com/questions/4721058/undefined-method-merge-for-2fixnum。这是最后的工作:<%= select(:carrier,“country”,@countries) - %>。谢谢! –