# How to use a form.check\_box  ?

**URL:** <https://discuss.rubyonrails.org/t/how-to-use-a-form-check-box/11755>\
**Category:** rubyonrails-talk\
**Created:** [April 16, 2007, 4:46pm UTC](https://discuss.rubyonrails.org/t/how-to-use-a-form-check-box/11755 "2007-04-16T16:46:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![AbsolutZero](https://avatars.discourse-cdn.com/v4/letter/a/d2c977/32.png) [@AbsolutZero](https://discuss.rubyonrails.org/u/AbsolutZero)\
**Post date:** [April 16, 2007, 4:46pm UTC](https://discuss.rubyonrails.org/t/how-to-use-a-form-check-box/11755/1 "2007-04-16T16:46:24Z")

</div>

Hey guys,

I'm trying to have a simple webform in my administration section to add a user. I have the text\_fields for adding the user's name, password, and password confirmation, but cannot seem to figure out the checkbox. I want it to be a single checkbox, that if clicked, will set the database column "access" to be "admin" and if its unchecked, set it to be "user". This is what I have so far but nothing is rendering just because of the check\_box line.

\<% form\_for :user do |form| %\> &nbsp;&nbsp;&nbsp;&nbsp;\<p\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<label for "user\_name"\>Name:\</label\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= form.text\_field :name %\> &nbsp;&nbsp;&nbsp;&nbsp;\</p\> &nbsp;&nbsp;&nbsp;&nbsp;\<p\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<label for="user\_password"\>Password:\</label\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= form.password\_field :password %\> &nbsp;&nbsp;&nbsp;&nbsp;\</p\> &nbsp;&nbsp;&nbsp;&nbsp;\<p\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<label for="user\_password\_confirmation"\>Confirm:\</label\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= form.password\_field :password\_confirmation %\> &nbsp;&nbsp;&nbsp;&nbsp;\</p\>

&nbsp;&nbsp;&nbsp;&nbsp;\<p\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<label for="user\_access"\>Access:\</label\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= form.check\_box :access, "admin", "user" %\> &nbsp;&nbsp;&nbsp;&nbsp;\</p\>

&nbsp;&nbsp;&nbsp;&nbsp;\<%= submit\_tag "Add User", :class =\> "submit" %\>

&nbsp;&nbsp;\<% end %\>

---

<div class="post-metadata">

**Author:** ![bphogan](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/bphogan/32/4186_2.png) [@bphogan](https://discuss.rubyonrails.org/u/bphogan)\
**Post date:** [April 16, 2007, 6:55pm UTC](https://discuss.rubyonrails.org/t/how-to-use-a-form-check-box/11755/2 "2007-04-16T18:55:37Z")

</div>

A quick glance at the api for check\_box shows this:

```ruby
check_box("puppy", "gooddog", {}, "yes", "no")

    <input type="checkbox" id="puppy_gooddog" name="puppy[gooddog]" value="yes" />

    <input name="puppy[gooddog]" type="hidden" value="no" />

```

Your code shows

\<%= form.check\_box :access, “admin”, “user” %\>

It looks like you left a parameter out, which is gonna cause problems.

Try

\<%= form.check\_box :access, {}, “admin”, “user” %\>

Note that the 2nd parameter is an empty hash… you’d use that to specify HTML options like :class=\>“my\_css\_class” and other stuff.

Let me know if that helps.

---

<div class="post-metadata">

**Author:** ![AbsolutZero](https://avatars.discourse-cdn.com/v4/letter/a/d2c977/32.png) [@AbsolutZero](https://discuss.rubyonrails.org/u/AbsolutZero)\
**Post date:** [April 16, 2007, 8:18pm UTC](https://discuss.rubyonrails.org/t/how-to-use-a-form-check-box/11755/3 "2007-04-16T20:18:31Z")

</div>

Thank you so much for the help, I appreciate it.

I tried that alteration, but I received this error when trying to access the page:

---

<div class="post-metadata">

**Author:** ![AbsolutZero](https://avatars.discourse-cdn.com/v4/letter/a/d2c977/32.png) [@AbsolutZero](https://discuss.rubyonrails.org/u/AbsolutZero)\
**Post date:** [April 16, 2007, 8:24pm UTC](https://discuss.rubyonrails.org/t/how-to-use-a-form-check-box/11755/4 "2007-04-16T20:24:53Z")

</div>

Thank you so much for the help, I appreciate it.

I tried that alteration, but I received this error when trying to access the page:

---

<div class="post-metadata">

**Author:** ![bphogan](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/bphogan/32/4186_2.png) [@bphogan](https://discuss.rubyonrails.org/u/bphogan)\
**Post date:** [April 16, 2007, 8:43pm UTC](https://discuss.rubyonrails.org/t/how-to-use-a-form-check-box/11755/5 "2007-04-16T20:43:59Z")

</div>

Does the ‘users’ table have a field called ‘access’

---

<div class="post-metadata">

**Author:** ![AbsolutZero](https://avatars.discourse-cdn.com/v4/letter/a/d2c977/32.png) [@AbsolutZero](https://discuss.rubyonrails.org/u/AbsolutZero)\
**Post date:** [April 16, 2007, 8:55pm UTC](https://discuss.rubyonrails.org/t/how-to-use-a-form-check-box/11755/6 "2007-04-16T20:55:34Z")

</div>

Its supposed to... I say that because my original table did not contain that field, but I added it with this migration:

---

<div class="post-metadata">

**Author:** ![bphogan](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/bphogan/32/4186_2.png) [@bphogan](https://discuss.rubyonrails.org/u/bphogan)\
**Post date:** [April 16, 2007, 9:13pm UTC](https://discuss.rubyonrails.org/t/how-to-use-a-form-check-box/11755/7 "2007-04-16T21:13:06Z")

</div>

No… it shouldn’t matter.

I’d open up the db and make sure the field is there. Then restart the web server (just in case it didn’t pick up the db change).

The code you have should work… :access should be the method on the object bound to the form (user). If it’s burping at you, it’s probably that the field isn’t there.
