Hi Mirek,
I''m not sure if this is the best way, but it should work. You can
construct
the assignment in a string like this (where name = "my_object"):
cmd = "@#{name} = #{name.classify}.find(params[:id])"
The value of cmd should then be "@my_object =
MyObject.find(params[:id])".
You can execute that string with the following:
eval(cmd)
If you want, you can compress it into one line. I broke it here out for
simplicity''s sake.
-Dan
On 9/12/07, Mirek Rusin
<ringley.ebay-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
wrote:>
>
> Hi there!
>
> How to set a local variable from a name inside a controller method?
>
> In other words an equivalent to:
>
> @my_object = MyObject.find(params[:id])
>
> when I have only
>
> name = "my_object"
>
> How to set it as an local variable (with @ thingy)
>
> ??? = Object.const_get(name.classify).find(params[:id])
>
> is there some kind of a method for the controller, something which
> looks similar to this:
>
> set_local_object name,
> Object.const_get(name.classify).find(params[:id])
> ^^^^^^^^^^^^^^^^
> ?
>
> Thanks,
> Mirek Rusin
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---