Forum: Ruby on Rails Typecasting and boolean attributes

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.
Derek H. (Guest)
on 2006-01-12 03:45
(Received via mailing list)
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
Kevin O. (Guest)
on 2006-01-12 04:20
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
Derek H. (Guest)
on 2006-01-12 07:05
(Received via mailing list)
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
Kevin O. (Guest)
on 2006-01-12 08:05
<%= 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
Derek H. (Guest)
on 2006-01-12 10:13
(Received via mailing list)
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
This topic is locked and can not be replied to.