2011-09-22 67 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) - %>。謝謝! –