Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby on Rails: Updating data after form submission
    text
    copied!<p>This is my form:</p> <pre><code>&lt;table&gt; &lt;tbody&gt; &lt;% form_for :HhActiveCarrier, @carriers, :url =&gt; { :action =&gt; "update" } do |f| %&gt; &lt;% for carrier in @carriers %&gt; &lt;tr&gt; &lt;%= render :partial =&gt; "summary_detail", :locals =&gt; {:carrier =&gt; carrier, :f =&gt; f} %&gt; &lt;/tr&gt; &lt;% end %&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;%= submit_tag "Update" %&gt; &lt;% end %&gt; </code></pre> <p>With my partial:</p> <pre><code>&lt;td class="tn"&gt;&lt;%= h(carrier.name.to_s()) -%&gt;&lt;/td&gt; &lt;td class="sc"&gt;&lt;%= h(carrier.country.to_s()) -%&gt;&lt;/td&gt; &lt;td class="sc"&gt;&lt;%= select(:carrier, "country", @countries) -%&gt;&lt;/td&gt; </code></pre> <p>This is the controller where I define the variables:</p> <pre><code>class ActiveCarriersController &lt; 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 =&gt; "index" ) end </code></pre> <p>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:</p> <blockquote> <p>OCIError: ORA-00904: "ID": invalid identifier: UPDATE hh_active_carriers SET name = 'AT&amp;T', country = null WHERE id = null</p> </blockquote> <p>How would I go about updating the attributes this? I am using ruby on rails 2.3.8.</p> <p>Edit:<br> Added parameters hash from development log:</p> <blockquote> <p>parameters: {"commit"=>"Update", "carrier"=>{"country"=>"USA"}, "action"=>"update", "controller"=>"active_carriers"}</p> <p>content_type: #</p> <p>accepts: [#, #, #]</p> <p>raw_post: "carrier%5Bcountry%5D=USA&amp;carrier%5Bcountry%5D=USA&amp;carrier%5Bcountry%5D=USA&amp;carrier%5Bcountry%5D=USA&amp;commit=Update"</p> <p>query_parameters: {}</p> <p>request_parameters: {"commit"=>"Update", "action"=>"update", "carrier"=>{"country"=>"USA"}, "controller"=>"active_carriers"}</p> </blockquote> <p>Edit3:</p> <p>Form: </p> <pre><code>&lt;table&gt; &lt;tbody&gt; &lt;% form_for :HhActiveCarrier, :url =&gt; { :action =&gt; "update" } do |f| %&gt; &lt;% for carrier in @carriers %&gt; &lt;tr&gt; &lt;%= render :partial =&gt; "layouts/summary_detail", :locals =&gt; {:carrier =&gt; carrier, :f =&gt; f} %&gt; &lt;/tr&gt; &lt;% end %&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;%= submit_tag "Update" %&gt; &lt;% end %&gt; </code></pre> <p>Partial:</p> <pre><code>&lt;td class="tn"&gt;&lt;%= h(carrier.name.to_s()) %&gt;&lt;/td&gt; &lt;td class="sc"&gt;&lt;%= h(carrier.id.to_s()) %&gt;&lt;/td&gt; &lt;td class="sc"&gt;&lt;%= h(carrier.country.to_s()) %&gt;&lt;/td&gt; &lt;td class="sc"&gt;&lt;%= f.collection_select :country, HhActiveCarrier::COUNTRIES, :to_s, :to_s %&gt;&lt;/td&gt; </code></pre> <p>Controller:</p> <pre><code>class ActiveCarriersController &lt; 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 =&gt; "index" ) end end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload