Call methods within a given block

Hi!

As I can see it is possible to access local variables inside a block,
why can’t I call methods from a block?

What I want to do is:
render(:update) { |page|
page.replace_html ‘filterkriterien’, :partial => ‘partials/filter’,
:locals => { :filter => filter }
}

filter is a getter method in the calling class. I receive a
NoMethodError (undefined method `filter’ for
#<#Class:0xb75c8300:0xb75c82b0>):

Alex.

Rendering in Rails is ‘magic’, in that it’s not actually getting run in
the
context of your controller. The renderer takes your controller’s
instance
methods ( @varname ) and makes them available to the template. Any
methods
you try to call will be looked for in the Controller’s Helper module.
Seeing
as you’re using #render in an action, you’re better off calling and
saving
the data before the #render call:

@filter = filter
render(:update) { |page|
page.replace_html ‘filterkriterien’, :partial => ‘partials/filter’,
:locals => { :filter => @filter }
}

Jason

Thanks for your help, my other workaround was for now:

caller = self
render :update do |page|
page.replace_html ‘filterkriterien’, :partial => ‘partials/filter’,
:locals => { :filter => caller.filter }
end

Alex.

You can designate controller methods be available to the views by using
‘helper_method’ in the controller. Like so:

def whatever_controller_method

whatever

end
helper_method :whatever_controller_method

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs