I have a mySQL table with a bit field called Monday, which is either 0 or 1, 1 being ''checked'' and 0 being ''not checked''. The default is 0. I have a check_box form helper: <%= f.label :Monday %><%= f.check_box :Monday %> Which outputs HTML as follows: <label for="service_Monday">Monday</label><input name="service[Monday]" type="hidden" value="0" /><input id="service_Monday" name="service[Monday]" type="checkbox" value="1" /> Whatever I do, whether check or not check, the value stored in the database is always 1, I have tried setting different default values but nothing seems to help. Can you advise what I''m doing wrong please? Thank ou -- Posted via http://www.ruby-forum.com/.
On Aug 4, 5:25 pm, 15characters 15characters <rails-mailing- l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Whatever I do, whether check or not check, the value stored in the > database is always 1, I have tried setting different default values but > nothing seems to help. > > Can you advise what I''m doing wrong please? Thank ouWhat''s in your controller ? Does the sql being executed look sane ? Fred> -- > Posted viahttp://www.ruby-forum.com/.
Frederick Cheung wrote:> On Aug 4, 5:25�pm, 15characters 15characters <rails-mailing- > l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > >> Whatever I do, whether check or not check, the value stored in the >> database is always 1, I have tried setting different default values but >> nothing seems to help. >> >> Can you advise what I''m doing wrong please? Thank ou > > What''s in your controller ? Does the sql being executed look sane ? > > FredYou mean this? def create @service_availability = ServiceAvailability.new(params[:service_availability]) respond_to do |format| if @service_availability.save flash[:notice] = ''ServiceAvailability was successfully created.'' format.html { redirect_to(@service_availability) } format.xml { render :xml => @service_availability, :status => :created, :location => @service_availability } else format.html { render :action => "new" } format.xml { render :xml => @service_availability.errors, :status => :unprocessable_entity } end end end or ... def new @service_availability = ServiceAvailability.new respond_to do |format| format.html # new.html.erb format.xml { render :xml => @service_availability } end end -- Posted via http://www.ruby-forum.com/.
In my app, I wanted different default value when checked, so I did something like this after looking at http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#M001612: <%= form.check_box :country_id, {}, "254, "0" %> and it has worked for me. In your case, it would be: <%= form.check_box :Monday, {}, "1", "0" %> although the way you have it should work..
On Aug 4, 5:37 pm, 15characters 15characters <rails-mailing- l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Frederick Cheung wrote: > def create > @service_availability > ServiceAvailability.new(params[:service_availability])You''re doing this, but that checkbox submits params[:service][:Monday] so it won''t ever be used. fred> > respond_to do |format| > if @service_availability.save > flash[:notice] = ''ServiceAvailability was successfully created.'' > format.html { redirect_to(@service_availability) } > format.xml { render :xml => @service_availability, :status => > :created, :location => @service_availability } > else > format.html { render :action => "new" } > format.xml { render :xml => @service_availability.errors, > :status => :unprocessable_entity } > end > end > end > > or ... > > def new > @service_availability = ServiceAvailability.new > > respond_to do |format| > format.html # new.html.erb > format.xml { render :xml => @service_availability } > end > end > -- > Posted viahttp://www.ruby-forum.com/.
khagimoto wrote:> In my app, I wanted different default value when checked, so I did > something like this after looking at > http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#M001612: > > <%= form.check_box :country_id, {}, "254, "0" %> > > and it has worked for me. > > In your case, it would be: > > <%= form.check_box :Monday, {}, "1", "0" %> > > although the way you have it should work..Hi, thank you for the suggestion. I changed it to: <%= f.label :Monday %><%= f.check_box :Monday, {}, "1", "0" %> ... and I''m still getting a value of 1 in the database, even when I don''t check the checkbox. Can''t figure this out, if I remove the checkbox completely, so it can''t be checked, sure enough, the value in the database becomes 0! -- Posted via http://www.ruby-forum.com/.
Frederick Cheung wrote:> On Aug 4, 5:37�pm, 15characters 15characters <rails-mailing- > l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> Frederick Cheung wrote: >> � def create >> � � @service_availability >> ServiceAvailability.new(params[:service_availability]) > > You''re doing this, but that checkbox submits params[:service][:Monday] > so it won''t ever be used. > > fredI wrote the HTML in wrong, I apologise, here is a copy and paste from the source: <label for="service_availability_Monday">Monday</label><input name="service_availability[Monday]" type="hidden" value="0" /> ... which is correct, but still isn''t producing the expected results. -- Posted via http://www.ruby-forum.com/.
Anyone, please? -- Posted via http://www.ruby-forum.com/.
Is it possible that you have another form field (hidden) with the same name defined later on in the form?
khagimoto wrote:> Is it possible that you have another form field (hidden) with the same > name defined later on in the form?<h1>New Service Availability</h1> <% form_for(@service_availability) do |f| %> <%= f.error_messages %> <p> <%= f.hidden_field :ServiceProviderID, :value => 1 %> </p> <p> <%= f.label :Service %><br /> <%= f.collection_select :ServiceID, ServiceProviderService.all, :ID, :Name, :include_blank => ''None'' %> </p> <p> <%= f.label :Monday %><%= f.check_box :Monday, {}, "1", "0" %><%= f.label :Tuesday %><%= f.check_box :Tuesday, {}, "1", "0" %><%= f.label :Wednesday %><%= f.check_box :Wednesday, {}, "1", "0" %><%= f.label :Thursday %><%= f.check_box :Thursday, {}, "1", "0" %><%= f.label :Friday %><%= f.check_box :Friday, {}, "1", "0" %><%= f.label :Saturday %><%= f.check_box :Saturday, {}, "1", "0" %><%= f.label :Sunday %><%= f.check_box :Sunday, {}, "1", "0" %> </p> <p> <%= f.label :TimeStart %><br /> <%= f.text_field :TimeStart %> <%= time_select(:TimeStart, "sunrise") %> </p> <p> <%= f.label :TimeFinish %><br /> <%= f.text_field :TimeFinish %> <%= time_select(:TimeFinish, "sunrise") %> </p> <p> <%= f.submit ''Create'' %> </p> <% end %> <%= link_to ''Back'', service_availabilities_path %> <%= link_to ''Service Providers'', service_providers_path %> -- Posted via http://www.ruby-forum.com/.
On Aug 5, 2:20 pm, 15characters 15characters <rails-mailing- l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Anyone, please?So have you figured out where this is going wrong ? Do the parameters that you see in the log look ok ? Does the sql your app execute ok ? Have you tried stepping through your app with the debugger to pinpoint where things are going wrong ? Fred> -- > Posted viahttp://www.ruby-forum.com/.
I''m scanning this thread, you mentioned that your output is: <label for="service_Monday">Monday</label><input name="service [Monday]" type="hidden" value="0" /><input id="service_Monday" name="service[Monday]" type="checkbox" value="1" /> You -cannot- have two input fields both containing the same name value. I assume the value from the last input field will be the one the browser uses. You have: <input id="service_Monday" name="service[Monday]" type="checkbox" value="1" /> Which means that even if it is not checked, the checkbox will return a value of 1. And being the last input with that name, your form will always have the value=1. What I think would be more helpful is: <input type=checkbox ... checked /> (not value="1") Also, write an javascript function that is fired onsubmit that checks both the checkbox and the hidden value and stores the approprite value in the parameter before it goes to the server. On Aug 4, 10:25 am, 15characters 15characters <rails-mailing- l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I have a mySQL table with a bit field called Monday, which is either 0 > or 1, 1 being ''checked'' and 0 being ''not checked''. The default is 0. > > I have a check_box form helper: > > <%= f.label :Monday %><%= f.check_box :Monday %> > > Which outputs HTML as follows: > > <label for="service_Monday">Monday</label><input name="service[Monday]" > type="hidden" value="0" /><input id="service_Monday" > name="service[Monday]" type="checkbox" value="1" /> > > Whatever I do, whether check or not check, the value stored in the > database is always 1, I have tried setting different default values but > nothing seems to help. > > Can you advise what I''m doing wrong please? Thank ou > -- > Posted viahttp://www.ruby-forum.com/.
If you look at my code included above, I''m not outputting that second input field <label for="service_Monday">Monday</label><input name="service [Monday]" type="hidden" value="0" /><input id="service_Monday" name="service[Monday]" type="checkbox" value="1" /> It just generates the second hidden field, I don''t know why, this is my code <%= f.label :Monday %><%= f.check_box :Monday, {}, "1", "0" %><%f.label :Tuesday %><%= f.check_box :Tuesday, {}, "1", "0" %><%= f.label :Wednesday %><%= f.check_box :Wednesday, {}, "1", "0" %><%= f.label :Thursday %><%= f.check_box :Thursday, {}, "1", "0" %><%= f.label :Friday %><%= f.check_box :Friday, {}, "1", "0" %><%= f.label :Saturday %><%= f.check_box :Saturday, {}, "1", "0" %><%= f.label :Sunday %><%f.check_box :Sunday, {}, "1", "0" %> -- Posted via http://www.ruby-forum.com/.
On Thu, Aug 6, 2009 at 10:55 AM, ghettoiam<iamghetto-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You -cannot- have two input fields both containing the same name > value.Well, actually, you can. :-) <http://www.w3.org/TR/html401/interact/forms.html#control-name> You just have to process appropriately. FWIW, -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org twitter: @hassan
Not true, and including the same parameter more than once is actually quite common. Rails adds the hidden input tag for you on purpose. It''s to get around the problem that when a checkbox is left unchecked, browsers don''t even send the form parameter in the request. So if the checkbox is left unchecked, you''ll get one value sent for the parameter - the "0" defined in the hidden field. If you check the box, you get two values sent - the "0" from the hidden field, and "1" for the check box, in that order. The latter will overwrite the former in Rails'' (i.e. Rack''s) parameter munging code. -andy On Aug 6, 1:55 pm, ghettoiam <iamghe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m scanning this thread, you mentioned that your output is: > > <label for="service_Monday">Monday</label><input name="service > [Monday]" > type="hidden" value="0" /><input id="service_Monday" > name="service[Monday]" type="checkbox" value="1" /> > > You -cannot- have two input fields both containing the same name > value. > > I assume the value from the last input field will be the one the > browser uses. > > You have: > <input id="service_Monday" > name="service[Monday]" type="checkbox" value="1" /> > > Which means that even if it is not checked, the checkbox will return a > value of 1. And being the last input with that name, your form will > always have the value=1. > > What I think would be more helpful is: > <input type=checkbox ... checked /> (not value="1") > > Also, write an javascript function that is fired onsubmit that checks > both the checkbox and the hidden value and stores the approprite value > in the parameter before it goes to the server. > > On Aug 4, 10:25 am, 15characters 15characters <rails-mailing- > > l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > I have a mySQL table with a bit field called Monday, which is either 0 > > or 1, 1 being ''checked'' and 0 being ''not checked''. The default is 0. > > > I have a check_box form helper: > > > <%= f.label :Monday %><%= f.check_box :Monday %> > > > Which outputs HTML as follows: > > > <label for="service_Monday">Monday</label><input name="service[Monday]" > > type="hidden" value="0" /><input id="service_Monday" > > name="service[Monday]" type="checkbox" value="1" /> > > > Whatever I do, whether check or not check, the value stored in the > > database is always 1, I have tried setting different default values but > > nothing seems to help. > > > Can you advise what I''m doing wrong please? Thank ou > > -- > > Posted viahttp://www.ruby-forum.com/.