Forum: Ruby on Rails Proper Handling of HTML Select

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.
4a52e82e5cf94f4d5c80580b7fcbff99?d=identicon&s=25 twshelton (Guest)
on 2005-12-01 03:14
(Received via mailing list)
Here's what I'm trying to accomplish...

I have a person model and that peron has a home_country.  Currently,
home_country is an INT in the table.  What I want to be able to do is
convert
the INT into a Country object when I pull it from the db.  In order for
the HTML
helper functions to work, I overloaded the object.inspect method to
provide
default method functionality.  If I called person.home_country it should
invoke
the inspect and return to me whichever method I define.  This works in
the
script/console however, it does not work when using the helper
functions.  I
#<Country:...>.

Couple of questions:  Is there a better way to do this?  And why do I
experience
different behavior in the console and the HTML results?

Thanks for your help.
3dd4b52a0946bd698b1d1635a46ea3a3?d=identicon&s=25 francois.beausoleil (Guest)
on 2005-12-01 14:47
(Received via mailing list)
Hello Thomas,

2005/11/30, Thomas Shelton <twshelton@hrtoolbox.com>:
> different behavior in the console and the HTML results?
The console inspects return values, while ERb uses to_s.  That is why
you see different results from the console than on the Web.

That being said, which helper are you using ?

You should be doing something like this:
<%= collection_select :person, :home_country_id, Country.find(:all),
:id, :name %>

See how I am saying the value is coming from home_country_id instead
of home_country ?  That's because when the values come back, the
home_country_id will be updated, and when the object is saved, the DB
will contain the right value.

If instead you said home_country, then you'd have a problem - you'd
try to assign a Fixnum to a Country instance - not good.

Hope that helps !
4a52e82e5cf94f4d5c80580b7fcbff99?d=identicon&s=25 twshelton (Guest)
on 2005-12-02 00:29
(Received via mailing list)
Francois Beausoleil wrote:
>>script/console however, it does not work when using the helper functions.  I
>
> try to assign a Fixnum to a Country instance - not good.
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails

Thanks Francois.  I'll give that a shot and see how it works.  I was
alos looking for feedback on the approach to make sure I wasn't heading
down the wrong path.

Thomas
This topic is locked and can not be replied to.