问题描述:

I'm trying to override an action in a controller defined by a Rails Engine.

It seems like I need to require the original file before reopening the class, like so:

require File.join(RAILS_ROOT, 'vendor/plugins/myplugin/app/controllers/some_controller')

class SomeController

def index

render :text => 'this is my index'

end

end

This makes sense, but that require is pretty ugly. Is there some sort of Rails magic that would allow me to avoid the initial require?

网友答案:

This is a complete guess...

Seems more of a load timing problem. As in, your file is getting loaded before the plug-in. Where is your action located? config/initializers? lib?

I'm not to sure when Rails Engines gets loaded so play around with the location (should work by putting it in lib).

Or, better yet, create your own plug-in with the changes and make sure it loads after the original.

And you probably want something more like:

SomeController.class_eval do

  def index
    ...
  end

end
相关阅读:
Top