Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i assign a value to a check box in erb?

<%= f.check_box:TYPE %> AB <br><br>

I have used the above mentioned in my code and when i click submit it takes 1 if checked and 0 if not checkedand it is stored in db. How can i store a string like AB if checked and nil if not checked and i want to store that string in db instead of 0 and 1?

like image 828
usr Avatar asked Aug 21 '12 04:08

usr


2 Answers

This is the definition of this helper method:

check_box (object_name, method, options = {}, checked_value = "1", unchecked_value = "0")

So I supose you need something like:

<%= check_box :type, {}, "AB", "nil" %> AB <br><br>

But in your rails application you get string 'AB' and string 'nil', that how it works.

like image 98
Danil Speransky Avatar answered Sep 19 '22 02:09

Danil Speransky


Rails 3.2 @ http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html check_box(object_name, method, options = {}, checked_value = "1", unchecked_value = "0")

ex. <%= f.check_box( :type, {}, "yes", "no") %>

like image 42
Yang Chu Avatar answered Sep 18 '22 02:09

Yang Chu