0

This is my form:

<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 %>

With my partial:

<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>

This is the controller where I define the variables:

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

All of this works. However, if I make a change to the drop down list in the form to this:

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

I get this error:

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

What am I doing wrong with my form select? I am using Ruby on Rails 2.3.8

Stackoverflow is telling me that I don't have much explanation for all the code i have, so I am just writing stuff. I don't really know what else to explain, so ask questions if you don't understand everything. Also I couldn't quote the error message because Stackoverflow kept telling me I had code that didn't have the code blocks, so I had to put code blocks around the error message.

Di Zou
  • 4,469
  • 13
  • 59
  • 88
  • I think you need to call options_for_select(@countries) to convert the array into selectable options. http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-options_for_select – lashleigh Sep 22 '11 at 21:47
  • I changed it to: <%= f.select(:country, options_for_select(@countries)) -%> and I still get the "undefined method `country' for #" error – Di Zou Sep 22 '11 at 21:51

1 Answers1

0

Try this: f.select("carrier", "country", options_for_select(@countries))

railscard
  • 1,848
  • 16
  • 12
  • So I tried: f.select("carrier", "country", options_for_select(@countries)). I got this error: undefined method `merge' for #. After reading this: http://stackoverflow.com/questions/4721058/undefined-method-merge-for-2fixnum. This is what finally worked: <%= select(:carrier, "country", @countries) -%>. Thanks! – Di Zou Sep 23 '11 at 13:23