在Ruby on Rails中遇到表单下拉列表时遇到问题

时间:2011-09-22 21:35:15

标签: 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一直告诉我我的代码没有代码块,所以我不得不在错误消息周围放置代码块。

1 个答案:

答案 0 :(得分:0)

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