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(:carrier, "country", @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

    def update
        carriers = HhActiveCarrier.find(:all)
        for carrier in carriers
            carrier.update_attributes(params[:country])
        end
        redirect_to( :action => "index" )
    end

What I want to happen is after I click the "Update" button, I want the new country selected from the drop down list to be put into the HHActiveCarrier model. With the code I have right now, I get this error:

OCIError: ORA-00904: "ID": invalid identifier: UPDATE hh_active_carriers SET name = 'AT&T', country = null WHERE id = null

How would I go about updating the attributes this? I am using ruby on rails 2.3.8.

Edit:
Added parameters hash from development log:

parameters: {"commit"=>"Update", "carrier"=>{"country"=>"USA"}, "action"=>"update", "controller"=>"active_carriers"}

content_type: #

accepts: [#, #, #]

raw_post: "carrier%5Bcountry%5D=USA&carrier%5Bcountry%5D=USA&carrier%5Bcountry%5D=USA&carrier%5Bcountry%5D=USA&commit=Update"

query_parameters: {}

request_parameters: {"commit"=>"Update", "action"=>"update", "carrier"=>{"country"=>"USA"}, "controller"=>"active_carriers"}

Edit3:

Form:

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

Partial:

<td class="tn"><%= h(carrier.name.to_s()) %></td>
<td class="sc"><%= h(carrier.id.to_s()) %></td>
<td class="sc"><%= h(carrier.country.to_s()) %></td>
<td class="sc"><%= f.collection_select :country, HhActiveCarrier::COUNTRIES, :to_s, :to_s %></td>

Controller:

class ActiveCarriersController < ApplicationController

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

    end


    def update
        #update code
        redirect_to( :action => "index" )
    end
end
8
  • Can you post the params hash, as it's submitted, from your dev log? The way I read your code, it looks like you're taking the country selected, and setting that to be the country for EVERY carrier, as opposed to pairing the correct country and carrier together. It's hard to tell without seeing what's actually being submitted. Thanks! Commented Sep 23, 2011 at 14:16
  • I made an edit. Is that what you are looking for? Commented Sep 23, 2011 at 14:27
  • Yes, I think this is what I was looking for. Commented Sep 23, 2011 at 14:34
  • Try taking out the = in <%= f.fields_for :carrier do |carrier_fields| %> to solve the first error about unexpected ')' Commented Sep 23, 2011 at 15:45
  • Or maybe adding = to the <% form_for ... %>` I can never remember which is correct, but I make that mistake all time. Commented Sep 23, 2011 at 15:54

1 Answer 1

2

A few things:

  1. Adjust your form so it uses the fields_for helper for each of the carriers (scroll down almost 1/2 way, for the code snippet that's labeled "Or a collection to be used:")
  2. Add a hidden field in your partial that indicates the ID of the carrier being updated (right now, your params hash doesn't include the ID of the record to be updated, so the update fails)
  3. Don't loop through all carriers in your controller. You want to loop through the hash instead.

So, the hash you want from the form should look something like:

params => {:carrier[1] => {:country => "USA", :id=>"5"}, carrier[2] => {:country => "Brazil", :id=>"17"}}

Then in your controller, you would loop through params[:carrier].each to update your carriers.

Sign up to request clarification or add additional context in comments.

2 Comments

I made an edit with what I tried. Any help would be appreciated. Thanks!
I made a couple more edits. I'm still not sure what I need to do to put in ActiveCarriersController.update to update my HhActiveCarrier model and the database. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.