這是我的形式:與表單時遇到下拉列表中的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一直告訴我我的代碼沒有代碼塊,所以我必須在錯誤消息的周圍放置代碼塊。
我認爲你需要調用options_for_select(@countries)到數組轉換成可選擇的選項。 http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-options_for_select – lashleigh
我將它改爲: