samedi 18 juillet 2015

How can I not sending anything to controller if user not checked checkbox

I have a form that have 2 kind of input radio button and checkbox. I'm using accepts_nested_attributes_for to do a nested attribute so, for the radio button it's work fine

<%= questions.each do |question| %> 
  <%= f.fields_for :user_response do |response| %>
   <% question.answers.each do |answer| %>
    <%= response.radio_button :answer_id, answer.id %>
   <% end %>
  <% end %>
<% end  %>

it's will sending the answer_id of the choice which user selected

but, on the checkbox

<%= questions.each do |question| %> 
 <% question.answers.each do |answer| %>
  <%= r_s.fields_for :user_response do |response| %>
   <%= response.check_box :answer_id, {}, answer.id, nil %>
  <% end %>
 <% end %>
<% end  %>

it's have some problem ,when user isn't check the checkbox it still send data with nil values of answer_id because of <%= response.check_box :answer_id, {}, answer.id, nil %>

my question is how can i send only the answer_id of checked check box.




Aucun commentaire:

Enregistrer un commentaire