Forum: Ruby on Rails Question about Mapping from Forms

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.
F2a8e4cc069558b818508c80052f2f5e?d=identicon&s=25 Brent Johnson (Guest)
on 2006-03-28 22:37
(Received via mailing list)
During a load or save in my controller I've manually been doing:
@myobject = MyObject.find(:first, :conditions => "id =
#{@params['id']}")

If I'm updating an existing object I load it, change stuff using
something like myobject.name = "@params['name']", etc etc, then call
save.  That's what I've done in similar situations for ages in other
development languages.  But, I had the urge to go back and look at
some of the online tutorials, where I noticed references like this:

<input type="hidden" id="myobject_id" name="myobject[id]" value="<%=
@myobject.id %>"/>

So it appears as though Rails can automagically populate an object
which can then be referred to with something like:

@myobject = @params['myobject']

At least that's what it SEEMS like.  But if I define a select like:

<select name="myobject[id]">
.... code to loop through options setting value to myobject ids ...
</select>

I just have that, and a submit button.  I have the @myobject =
@params['myobject'] in the resulting controller, but if I try and view
@myobject.id in the view I get something like "-606936886" which is
definitely not valid.

Can rails load the object up for me or should I just go back to doing
the find() calls?  Using myobject[property] to name inputs for calling
a scaffolded update() seems to work just fine.

I'd appreciate any little bit of info you may have.

Thanks!

- Brent
This topic is locked and can not be replied to.