Injecting actions into controllers

Hi,

I’m trying to write a plugin that should inject an action into the
controller.
Please note, not just an instance method, but also an action, one of
those
that are listable by running ControllerClassName.action_methods.

I wrote the following code:

def render_field_search(name, object, controller, options = {})
controller.class.class_eval do
define_method("#{name}_flt") {
raise ‘Here iam!’
}
end

this should define the new method as an action too

controller.class.action_methods << “#{name}_flt”
[…]
end

It works if I try to call the method from inside render_field_search()
itself,
it fails if I call it as normal HTTP request:

ActionController::UnknownAction
(No action responded to reservation_customer_id_flt):

/actionpack-1.12.3/lib/action_controller/filters.rb:368:
in `perform_action_without_benchmark’

Does anyone know why this happens?

Thanks.

Cheers,
Marco

Keywords for future searches:
inject action method controller plugin class_eval eval

On 24/07/06, Marco L. [email protected] wrote:

I’m trying to write a plugin that should inject an action into the controller.
Please note, not just an instance method, but also an action, one of those
that are listable by running ControllerClassName.action_methods.

[…]

It works if I try to call the method from inside render_field_search() itself,
it fails if I call it as normal HTTP request:

The problem should be that for every requests a new instance of the
controller is created. So the changes made by class_eval and
define_method are lost. I’m trying to workaround the issue defining a
class method, but now the question is:

Can I call a Controller class method from an HTTP request?

and

How can I call a Controller class method from an HTTP request?

Marco

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