Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Adding variables to a binding

Reply
Thread Tools

Adding variables to a binding

 
 
marcus
Guest
Posts: n/a
 
      02-11-2006
I need to run an ERB template (a normal Rails view template) in a Rails
model (and then write the result to a file). The problem is that I need
to run it in the scope of the calling controller. So I was thinking of
creating a binding in the controller and pass into the model and use the
binding there. I do however need to add variables from the model scope
into the binding. How do I do this? Thought of using eval with the
binding but it only seems to take an input string to eval (a block would
have been nice I think).

The reason I do this is that the model in question is a STI model and
each sub class may need to add different variables. If you can think of
a better way to accomplish what I want I'll be glad to hear

Thanks,

/Marcus





 
Reply With Quote
 
 
 
 
marcus
Guest
Posts: n/a
 
      02-11-2006
Caleb Clausen skrev:
> On 2/11/06, marcus <(E-Mail Removed)> wrote:
>
> How about this instead:
>
> def outerscope
> vars={}
> innerscope(vars)
> p vars[:foo],vars[:bar],vars[:baz]
> end
>
> def innerscope(vars)
> vars[:foo]=vars[:bar]=vars[:baz]=1
> end
>

The problem is that ERB#result(binding) takes a binding so I need the
binding. The templates used in this case relies on Rails rules (member
variables in a controller are available in the ERB template) and I need
to create a binding containing everything from the controller scope (and
then add the new needed stuff from the model).

What about passing a block from outer scope and call it from inner scope
and pass in a Hash with variables from the inner scope, then loop over
the variables and add those as instance members (using instance_eval),
create the binding (inside the block) and then invoke ERB.result. Could
that work?

/Marcus



 
Reply With Quote
 
 
 
 
David Vallner
Guest
Posts: n/a
 
      02-12-2006
D=C5=88a Sobota 11 Febru=C3=A1r 2006 23:12 marcus nap=C3=ADsal:
> I need to run an ERB template (a normal Rails view template) in a Rails
> model (and then write the result to a file). The problem is that I need
> to run it in the scope of the calling controller. So I was thinking of
> creating a binding in the controller and pass into the model and use the
> binding there. I do however need to add variables from the model scope
> into the binding. How do I do this? Thought of using eval with the
> binding but it only seems to take an input string to eval (a block would
> have been nice I think).
>
> The reason I do this is that the model in question is a STI model and
> each sub class may need to add different variables. If you can think of
> a better way to accomplish what I want I'll be glad to hear
>
> Thanks,
>
> /Marcus


I -think- instance variables get exported from the ERB template into the=20
calling method.

David Vallner


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
Embedded python adding variables linking to C++-Variables / callbacks iwl Python 3 12-07-2006 08:54 PM
GridView binding - how to stop initial binding Amit ASP .Net 6 10-24-2006 08:06 AM
Adding variables to variables lmo863 C++ 1 08-03-2005 06:49 PM
Data Binding - using inline code vs. functions vs. straight binding Jordan ASP .Net 2 02-10-2004 08:32 PM



Advertisments