Topic: Storing CheckBox Values To Datastore
Hello:
I ran into some interesting behavior. I have a form with some checkboxes on it. I have a database table attribute that corresponds to each checkbox value. So, if the box is checked,
I want to store 1 in the attribute. If unchecked, I want to store 0 in the attribute. I noticed that if the checkbox is unchecked, then there is no attribute/value for that checkbox passed in the params list to the controller. Right now, I have a solution for this in that I just populate a hidden input value with 0 or 1 and pass that. However, I have a bunch of these check boxes and for each one, I need to create a hidden input value.
A nicer solution would be to check in the controller whether the attribute/value pair exists
in the params list. If not, then I could manually populate the object attribute so that it gets saved to the database.
Here is an example of my create method and a comment of the code I need. What is the line of code that I need here?
def create
@assemblytest = Assemblytest.new(@params[:assemblytest])
@assemblytest.assembly_id = cookies[:selectedAssemblyID]
# How do I check the existence of a param here in the @params[:assemblytest] list?
if @assemblytest.save
redirect_to :action => 'list'
else
render_action 'new'
end
end