How to use a form.check_box ?

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| %>
    <p>
      <label for "user_name">Name:</label>
      <%= form.text_field :name %>
    </p>
    <p>
      <label for="user_password">Password:</label>
      <%= form.password_field :password %>
    </p>
    <p>
      <label for="user_password_confirmation">Confirm:</label>
      <%= form.password_field :password_confirmation %>
    </p>

    <p>
        <label for="user_access">Access:</label>
        <%= form.check_box :access, "admin", "user" %>
    </p>

    <%= submit_tag "Add User", :class => "submit" %>

  <% end %>

A quick glance at the api for check_box shows this:

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.

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:

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:

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

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

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.