Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Allow users to input boolean values

I am building an app that allows users to register/book attendance in events. Part of the registration form is finding out whether the user wants to book a place for accommodation, I have added that column to the registration model accepting boolean values. I can't seem to be able to allow the users to input boolean values through the form. I tried something like

<%= f.label :accommodation_request%>
<%= f.checkbox :accommodation_request%>

I also tried using radio_buttons and so. All failed

4 Answers

Are you using the standard form_for? Or are you using simple_form?

The column in the database needs to be set to boolean, and then what you posted is almost correct for a form_for. (See below) I would also recommend a default value on your boolean, which you do in your migration (default: false).

      <%= f.label :accommodation_request %>
      <%= f.check_box :accommodation_request %>

Notice you need the underscore for check_box. I highly recommend using the rails docs for form helpers (it's so very helpful!):

http://guides.rubyonrails.org/form_helpers.html

I am using form_for. I did a typo up there, I did use check_box but it didn't work. I can add a deafult value using before_save in active record, right?

Using a before_save is not the best method, since when the user sees the form, there is no default value to either check or uncheck the check_box.

What do you meant the check box didn't work? Is it not showing in the form? Or is it not saving to the database?

In order to diagnose, I need to see the schema.rb table for this model AND your form_for for this form.

You can take a look at the entire code here

I would just try:

<%= f.label :accomodation_request %>
<%= f.check_box :accomodation_request %>

What happens when you do that?

Accomodation_request gets registered as nil in the database

make sure you permit it in the params (for strong parameters) I also recommend a default value in the database.

How can I add a deafult value in the database to an already created column in the database? And how can I do that while creating a model/new column?