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| %>


<label for “user_name”>Name:
<%= form.text_field :name %>



Password:
<%= form.password_field :password %>



Confirm:
<%= form.password_field :password_confirmation %>

<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” />

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:


undefined method `access’ for #User:0x49b0fd8

Extracted source (around line #22):

19:
20:


21: Access:
22: <%= form.check_box :access, {}, “admin”, “user” %>
23:


24:
25: <%= submit_tag “Add User”, :class => “submit” %>

It seems to think that :access is a method… its actually the name of
the column in the database. Is that wrong to put there? Do I need a
method there that assigns the database the value?

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:


undefined method `access’ for #User:0x49b0fd8

Extracted source (around line #22):

19:
20:


21: Access:
22: <%= form.check_box :access, {}, “admin”, “user” %>
23:


24:
25: <%= submit_tag “Add User”, :class => “submit” %>

It seems to think that :access is a method… its actually the name of
the column in the database. Is that wrong to put there? Do I need a
method there that assigns the database the value?

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:


class UserAccess < ActiveRecord::Migration
def self.up
add_column :users, :access, :string, :default => “user”
end

def self.down
remove_column :users, :access
end
end


I executed “rake db:migrate” but its still not showing up… Maybe my
problem was due to my migration not editing the table? Does it matter
that there is already data in the table?

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.