Extending controllers with actions from external files?

Hey :slight_smile:

I have some auto_complete_for_blah_blah actions that are used in
different controllers. In the spirit of DRY, I’d like to separate
these actions in unique files which I then load into the controllers
that use them.

Okay, so the way I can do it now is for instance
myapp
± lib
± auto_complete_for
± user_printed_name.rb

And then, I can
require ‘auto_complete_for/user_printed_name’

in the controllers that use this auto_completer.

However, even though the method is available to the controller, I
still get “no action responded to
‘auto_complete_for_user_printed_name’” when I call the action. That
is, it’s available as a local method to the controller, but I’d really
like for it to appear as an action, so that I don’t have to create a
proxy, like,

def auto_complete_for_user_printed_name
shared_auto_complete_for_user_printed_name
end

How would I do that?

Thanks :slight_smile:

Daniel

Daniel Smedegaard B. wrote the following on 08.03.2007 11:11 :

+- auto_complete_for
  +- user_printed_name.rb

And then, I can
require ‘auto_complete_for/user_printed_name’

in the controllers that use this auto_completer.

From memory, you should:
include AutoCompleteFor::UserPrintedName

assuming this the module name defined in
lib/auto_complete_for/user_printed_name.rb

You probably don’t need the require if your module naming matches your
directory structure like in the example above (Rails figures the needed
require for itself).

Lionel

On Mar 8, 11:50 am, Lionel B. [email protected]
wrote:

Close! Following your advice and a little fiddling, I found that with,

lib/auto_complete_for.rb:
module AutoCompleteFor
module UserPrintedName
def auto_complete_for_user_printed_name
// implementation
end
end
end

I can use the syntax you describe,

include AutoCompleteFor::UserPrintedName

and have the actions implemented!

Thank you very much! :smiley:

Daniel :slight_smile:

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