Forum: Ruby on Rails Checkboxes

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
41c8b1c51aa4074afbc0c912a0fdc85c?d=identicon&s=25 Kyle Stegerwald (Guest)
on 2006-12-25 06:02
I've got a User model with an ActiveRecord method called 'isAdmin' that
determines wether or not that user is an administrator. And in order to
create these users I have new_user.rhtml paired with _user_form.rhtml,
in which there is a checkbox that is supposed to determine the value of
'isAdmin' - 0 or 1. I've tried several variations of things I found in
my Rails book and on the web, and absolutely nothing has worked thus
far. When I turn on validation in the User model, I keep getting told
that my checkbox is blank, which is impossible, since if it's checked
isAdmin = 1 and if it's not checked isAdmin = 0.

Any suggestions?
70225136eacd3d870f64e03bff678655?d=identicon&s=25 Russell Norris (Guest)
on 2006-12-26 14:05
(Received via mailing list)
I've run across this. If you're using the form_for(:model) format it
seems
to generate the necessary hidden input with value=0 whereas the
check_box_tag didn't. If I recall correctly. Once I moved to the
form_for
format those kinds of problems haven't popped up again.

Hope that helps.

RSL
41c8b1c51aa4074afbc0c912a0fdc85c?d=identicon&s=25 Kyle Stegerwald (Guest)
on 2007-01-08 19:42
Russell Norris wrote:
> I've run across this. If you're using the form_for(:model) format it
> seems
> to generate the necessary hidden input with value=0 whereas the
> check_box_tag didn't. If I recall correctly. Once I moved to the
> form_for
> format those kinds of problems haven't popped up again.
>
> Hope that helps.
>
> RSL

I tried the form_for model using this code and I'm still getting the
"isAdmin can't be blank" error, despite the fact that I know that the
paramaters are being passed correctly, i.e. it shows me that I'm passing
isAdmin as either a 0 or a 1.

Anyway, here's the code... ignore the html.

<% form_for :user, @user, :url => { :action => "create_user" } do |u| %>
<tr><td>Username:</td><td><%= u.text_field :name %></td></tr>
<tr><td>Password:</td><td><%= u.text_field :password %></td></tr>
<tr><td>Grant admin powers? <%= u.check_box :isAdmin %></td></tr>
</table>
<br>
<input type = "submit" value = "Create New User">
<% end %>
This topic is locked and can not be replied to.