Helper method for models, controllers and views?

You can put stuff in lib/ and "require" it from config/environment.rb

Typically you would define methods in a module that would be mixed in
when needed using "include".

Bob Showalter wrote:

There must be a place for these, but does anyone know where?

You can put stuff in lib/ and "require" it from config/environment.rb

Typically you would define methods in a module that would be mixed in
when needed using "include".

Hmm, can't get this to work...

I made a file called general_helpers.rb and put it in my lib folder. It
has this:

module GeneralHelpers
  def hello
    "hello world!"
  end
end

then, in one of my views, i have

include 'general_helpers'

include GeneralHelpers

<span><%= hello %></span>

And this generates an "unknown method or attribute "hello"" error.

(I already do something similar with a module called ModelExtensions, in
lib/model_extensions.rb, which i require in my environment file. The
methods in there can be used by models, but not views or controllers.)

Under Rails, you don't even have to require 'general_helpers' as it will be autoloaded.

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

Bob Showalter wrote:
>> There must be a place for these, but does anyone know where?
> You can put stuff in lib/ and "require" it from config/environment.rb
>
> Typically you would define methods in a module that would be mixed in
> when needed using "include".

Hmm, can't get this to work...

I made a file called general_helpers.rb and put it in my lib folder. It
has this:

module GeneralHelpers
  def hello
    "hello world!"
  end
end

then, in one of my views, i have

include 'general_helpers'

wrong syntax. include takes a module name.

<span><%= hello %></span>

You'll have to put

   include GeneralHelpers

into class ApplicationHelper (in app/helpers/application_helper.rb) in
order for those to be available in your views.

I think that you actually want to extend the view object with this method so do:

extend GeneralHelpers

and see if that fixes the problem. 'include' is to give instance methods to a class/module while 'extend' is to give methods to the object itself (frequently for class methods, but also on individual objects).

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

Yes, you can include a module in another module.

I'm not familiar with ModelExtensions, but something like this should
work (put it in your general_helpers.rb)

module ActiveRecord
  class Base
    include GeneralHelpers
  end
end

Bob Showalter wrote:
>> > models?
>> Btw, this might be to do with the fact that ModelExtensions is itself a
>> module - can you include a module in another module?
>
> Yes, you can include a module in another module.
>
> I'm not familiar with ModelExtensions, but something like this should
> work (put it in your general_helpers.rb)
>
> module ActiveRecord
> class Base
> include GeneralHelpers
> end
> end

That doesn't seem to work for me...let me just run through what i have
now as i may have got confused somewhere.

I have a file in the lib folder called "general_helpers.rb" with

  module GeneralHelpers
    def hello
      "hello world!"
    end

  end

  module ActiveRecord
    class Base
      include GeneralHelpers
    end
  end

And i have a method in my Movie model that looks like this:

  def hello_test
    return hello
  end

when i call this method on an instance of type Movie (in a view) i get

"undefined local variable or method `hello' for #<Movie:0x44c9a38>"

I tried restarting the server just in case, but it didn't help...

If you go into script/console and do this, it should work:

   >> GeneralHelpers
   GeneralHelpers
   >> Movie.new.hello
   "hello world!"

The problem is that nothing is causing general_helpers.rb to be
loaded. I forced it to load by referencing the module name as the
first thing.

You'll need to go ahead and add to config/environment.rb (put it at the bottom)

  require 'general_helpers'