On Mon, 2009-07-27 at 00:16 -0700, Scott Windsor wrote:
> I can't quite figure out how to pass context into Procs such that
> state can be passed into the action I'm linking to.  For example:
> 
> ['http://foo.com', 'http://bar.com'].each do |s|
>   link(s, :click => Proc.new{ window { image s } })
> end

The issue isn't so much that s is no longer in scope, but rather the
variable is getting captured rather than the specific variable.

What I would suggest doing is define a method in your application like
this:

 def make_image_link(s)
   link(s, :click => Proc.new { window { image s } })
 end

And then set things up like this:

 ['http://foo.com', 'http://bar.com'].each do |s|
   make_image_link(s)
 end

This way, rather than a loop variable (which gets recycled between
loops), the Proc will capture a variable unique to the call to
make_image_link.

-mental

Reply via email to