render :update and controller private methods

Probably asked and answered, but...

Why are controller private methods inaccessible inside the block passed to render :update ?

This does not work:

class MyController < ApplicationController

  def some_action     render :update do |page|       page.replace_html 'an_element', some_private_method     end   end

  private

  def some_private_method     return 'data'   end end

I think this works:

class MyController < ApplicationController

  def some_action    some_private_method     render :update do |page|       page.replace_html 'an_element', variable_name     end   end

  private

  def some_private_method     variable_name = return 'data'   end end

I think this works:

Guaranteed not to work - you're just creating a local_variable in a
completely different scope. Like i said in a similar thread, a render :update block behaves like a
normal view: instance variable are copied over. If you want controller
instance methods to be available, they need to be helper methods (see
documentation for helper_method)

Fred

Frederick Cheung wrote:

Like i said in a similar thread, a render :update block behaves like a normal view: instance variable are copied over. If you want controller instance methods to be available, they need to be helper methods (see documentation for helper_method)

That's useful to know.

An alternative is to call controller instance methods from inside the render block like "controller.meth", or "controller.send(:meth)" for private methods.

Frederick Cheung wrote: > Like i said in a similar thread, a render :update block behaves like a > normal view: instance variable are copied over. If you want controller > instance methods to be available, they need to be helper methods (see > documentation for helper_method)

That's useful to know.

Yes, but somewhat unexpected. But

An alternative is to call controller instance methods from inside the render block like "controller.meth", or "controller.send(:meth)" for private methods.

Thanks for that. Ruby is the only language I have that used that supports closures while also allowing methods to be called without an explicit object. I expected that 'self' would be bound inside the closure at the time the closure was defined.