html - How to check if a check_box in simple_form has been checked -
here's html produces
f.check_box :tos
produces
<input name="user[tos]" type="hidden" value="0"> <input id="user_tos" name="user[tos]" type="checkbox" value="1">
what need in controller check if it's been checked?
assuming mean want find out if checked upon submission, it's value via params[:user][:tos]
. submitted data form stored in params hash, , location equivalent name attribute of input. instance:
if params[:user][:tos] == "1" # whatever here if checked else # whatever here if unchecked end
if need react state of being checked on web page, cannot done controller, , must use javascript. like:
if (document.getelementbyid('user_tos').checked == 1){ // whatever here if checked } else { // whatever here if unchecked }
addendum
when receiving parameter via controller, don't use value create new object (i.e. thing.create( thing_value: params[:user][:tos] )
). if y our goal, should "strong parameters," , how rails implements them.
addendum 2
thanks ruby's duck typing (dynamic typing) , nature of params hash, url encoding, etc. integers sent via params, in case params[:user][:tos]
, mutated string. you'll need check "1" (the string form) not 1 (the int form).
Comments
Post a Comment