Proper Handling of HTML Select


#1

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.


#2

Hello Thomas,

2005/11/30, Thomas S. removed_email_address@domain.invalid:

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 !


#3

Francois B. 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
removed_email_address@domain.invalid
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