Note that there are some explanatory texts on larger screens.

plurals
  1. PORadio button value in rails form_for not being part of params[:model]
    primarykey
    data
    text
    <p>I have an model (CustInfo), which contains 3 attributes: cust_name, age, gender. In my edit view, I use form_for for form submission:</p> <pre><code>&lt;%= form_for @cust[0] do |cust| %&gt; &lt;label for="cname"&gt;Customer name: &lt;/label&gt; &lt;%= cust.text_field :cust_name, :size =&gt; 20 %&gt; &lt;label for="age"&gt;Customer age: &lt;/label&gt; &lt;%= cust.text_field :age, :size =&gt; 5 %&gt; &lt;label for="gender"&gt;Gender &lt;/label&gt; &lt;%= radio_button_tag(:gender, "F", :checked =&gt; (:gender == 'female')? true:false) %&gt;&lt;span style="float:left"&gt;F&lt;/span&gt; &lt;%= radio_button_tag(:gender, "M", :checked =&gt; (:gender == 'male')? true:false) %&gt;&lt;span style="float:left"&gt;M&lt;/span&gt; &lt;%= cust.submit "Save" %&gt; &lt;% end %&gt; </code></pre> <p>In my controller I have</p> <pre><code>def update if Cust.update_all(params[:custinfo]) flash[:notice] = "Successfully updated!" redirect_to :action =&gt; "index" else flash[:notice] = "There are errors in the form, please try again" render :action =&gt; "edit" end end </code></pre> <p>When I press submit button, the form submitted, and its parameters are like this: </p> <pre><code>Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"I9QEdP1mRr65wT9TRbzNokkaa+LHVyPfmgWJMKoup", "custinfo"=&gt;{"age"=&gt;"16", "cust_name"=&gt;"jsmith"}, "gender"=&gt;"M","commit"=&gt;"Save"}. </code></pre> <p>It looks like gender does get passed through params, but it's not within the params[:model], in this case params[:custinfo] and that's why the update function wouldn't update this value (because I only had Cust.update_all(params[:custinfo])). My question is that why only this radio button isn't in params[:custinfo], while the other two are? And how can I pass the radio button value inside params[:custinfo]? Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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