Stupid checkboxes

I’m trying to pass a true/false from my account/signup page to the
account controller but I can’t even get the checkbox to work on the view
itself. Although I’m trying not to use a database field I went ahead
because I get getting unknown method or unknown variable errors without
it.

Here’s the code:

<%= check_box_tag ‘user’, ‘user_type’, :checked => ‘checked’ %>

<% warn @user.user_type %>

Simple, eh? I figure that the @user.user_type would give me a 1 or 0 or
true or false or something but all I get is nil. So @user.user_type?
always yields false no matter whether the checkbox is ticked or not.

Also tried it with just plain check_box but the same thing happens. It
can’t be that hard, can it?

Hi Vince,

Vince W. wrote:

I’m trying to pass a true/false from my account/signup
page to the account controller but I can’t even get the
checkbox to work on the view itself.

Here’s the code:

<%= check_box_tag ‘user’, ‘user_type’, :checked => ‘checked’ %>

For starters, you’re passing the arguments for check_box, not
check_box_tag.
In case you don’t have the URL, it’s http://api.rubyonrails.org/. Look
in
the bottom left panel (methods).

<% warn @user.user_type %>

Simple, eh? I figure that the @user.user_type
would give me a 1 or 0 or true or false or
something but all I get is nil. So @user.user_type?
always yields false no matter whether the
checkbox is ticked or not.

Rails view code produces html that will be served back to the visitor’s
browser for display. It looks like you’re expecting JS-like behavior.
When
the code in your view is executed, nothing has been entered by the user.
Thus your nil result. One ‘rule of thumb’ I developed / found helpful
early
on is that instance variables are for ‘pushing’ info to the visitor’s
brower, and params are for ‘pulling’ info the visitor enters from the
browser back in to the app.

Also tried it with just plain check_box but
the same thing happens.

Don’t know exactly what you mean by ‘tried’. Did you, for example, also
switch from ‘start_form’ to ‘start_form_tag’? It needs to be
consistent.

It can’t be that hard, can it?

Once you get in the flow, it’s incredibly easy. Swimmin’ upstream
sucks,
though.

Best regards,
Bill

Bill W. wrote:

When
the code in your view is executed, nothing has been entered by the user.
Thus your nil result. One ‘rule of thumb’ I developed / found helpful
early
on is that instance variables are for ‘pushing’ info to the visitor’s
brower, and params are for ‘pulling’ info the visitor enters from the
browser back in to the app.

I had tried it with warn @params[user][user_type] too but was getting
the same error. Anyway, based on the above I went on the assumption
that it was just problematic in the view and sure enough that’s it.
Putting if @user.user_type? in my controller behaves as expected.

Thanks!