Reputation: 4638
I have a form, where I defined a vanilla html checkbox
<%= form_tag charges_path, id: 'chargeForm' do %>
(...)
<input type="checkbox" id="agreed_to_terms" name="agreed_to_terms" /><label class="inputclass" for="agreed_to_terms">Agree to Terms</label>
<% end %>
model
class Product < ActiveRecord::Base
(...)
validates_acceptance_of :agreed_to_terms
When POST, the params:
"agreed_to_terms"=>"on"}
controller
if charge["paid"]
@product.update(status: "sold",
email: params[:email],
first_name: params[:first_name],
last_name: params[:last_name]
agreed_to_terms: params[:agreed_to_terms])
Transaction rollsback.
ActiveRecord says it needs to be true or false, not on or off.
How do I get this to work without doing this
if charge["paid"]
@product.update(status: "sold",
email: params[:email],
first_name: params[:first_name],
last_name: params[:last_name]
@product.update_attribute(:agreed_to_terms, "true")
Which is wrong for so many reasons
Upvotes: 0
Views: 170
Reputation: 4638
I tried to reproduce the problem in the rails console, setting agreed_to_terms
to all of the following
t
"t"
true
"true"
1
"1"
accepted
"accepted"
to no avail.
Turns out, for some odd reason, I needed to specify in the validation what value the field should accept
class Product < ActiveRecord::Base
(...)
validates_acceptance_of :agreed_to_terms, accept: true
Upvotes: 0
Reputation: 17834
To go in details I need your form code, but for a quick fix you can do this
agreed_to_terms: (params[:agreed_to_terms].eql?('on') ? true : false)
or
In your form add the following lines instead of <input type="checkbox" id="agreed_to_terms" name="agreed_to_terms" />
<%= hidden_field_tag "agreed_to_terms", false %>
<%= check_box_tag "agreed_to_terms", checked = true %>
When the checkbox is unchecked it will pass false
, true
otherwise
Upvotes: 1