Typecasting and boolean attributes


#1

I have 2 radio buttons like this:

<%= radio_button ‘group’, ‘public’, true %>
<%= radio_button ‘group’, ‘public’, false %>

They hold the correct values when viewing the @group object. However,
when updating, it does not appear that the params[:group][:public]
value is being typecast correctly.

As params[:group][:public] = “true”, “true” should be resolved to a
“1” in the database. However, this is not occurring.

Has anyone else seen this issue or have a suggested fix?

Derek

Derek H.
HighGroove Studios - http://www.highgroove.com
Atlanta, GA | San Mateo, CA
Keeping it Simple.
404.593.4879


#2

Derek H. wrote:

when updating, it does not appear that the params[:group][:public]
value is being typecast correctly.

As params[:group][:public] = “true”, “true” should be resolved to a
“1” in the database. However, this is not occurring.

What is typecasting?

Seriously, radio buttons return string literals back in the params, so
true comes back as “true”. If you want it to map to 1 or 0, set the
radio button to return “1” or “0”.

in ruby at least…

true.to_s => “true”

(true == 1) => false

_Kevin


#3

But that’s the problem…if I set the value of the radio button to
“1”, it won’t show up as selected when rendering the view because
Rails will only mark it selected if its value is “true.”

So if I set the value as 1, the attribute value is saved correctly to
the DB…but then the button won’t be selected as the attribute it is
tied to has a value of “true.”

As you said:
true == 1.

  • Derek

  • Derek

On 1/11/06, Kevin O. removed_email_address@domain.invalid wrote:

true comes back as “true”. If you want it to map to 1 or 0, set the

Posted via http://www.ruby-forum.com/.


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Derek H.
HighGroove Studios - http://www.highgroove.com
Atlanta, GA | San Mateo, CA
Keeping it Simple.
404.593.4879


#4

<%= radio_button ‘group’, ‘public’, “1” %>
<%= radio_button ‘group’, ‘public’, “0” %>

This works…

the params hash will look like this {… “group” => {“public”=>“1”}}
when the first one is selected.

The first radio button will be selected if …

group.public == “1”

Hope that helps…

_Kevin


#5

Yeah…I’ll give that another shot…making I was tired and just missed
it.

Thanks for your help Kevin,

Derek

On 1/11/06, Kevin O. removed_email_address@domain.invalid wrote:

group.public == “1”
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Derek H.
HighGroove Studios - http://www.highgroove.com
Atlanta, GA | San Mateo, CA
Keeping it Simple.
404.593.4879