Helper method for models, controllers and views?

Does anyone know where i can put a helper method so it can be accessed
from ANYWHERE? ie from a model, a controller or a view?

Currently i’ve got model helper methods in lib/model_extensions.rb,
controller helper methods in application.rb and view helper methods in
application_helper.rb.
But there’s lots of things (usually text helpers) that i want to be
available to anything.

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

thanks
max

Hi max.
I tried to understand you :smiley: Because my english not enough.

Anyway the helpers are in our helper directories as you know.
We can use them in our views. But the important point,
if you wanna write your own helper and you wanna use it
all your views you must write your helper to application_helper.rp
For example.
i use this def
----------------- application_helper.rb-----------------
def my_helper(number)
number_to_currency(number)
end

and in my view
------------ list.rhtml ---------

Price: <%= my_helper(book.price) %>

that is it.

my explorer show this:

Price:

Alper Türker wrote:
But the important point,

if you wanna write your own helper and you wanna use it
all your views you must write your helper to application_helper.rp

Hi Alper - i’m sorry, but you did misunderstand my question. I was
asking if it’s possible to put a single helper method in a single place
so that it can be used in all views, models and controllers.

On 9/20/07, Max W. [email protected] 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”.

Bob S. wrote:

On 9/20/07, Max W. [email protected] 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’

<%= hello %>

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.)

There are probably several ways to do this. Me, I’d just make it a class
method on the model (odds are, it’s really business logic that belongs
to the model).

class MyModule
def self.whatever

whatever

end
end

Somewhere else, call:

MyModule.whatever()

It won’t have access to instance data and methods with self. of course,
but if you want it to be callable from model, view, and controller, then
what instance data would it have access to anyway? Model and
view/controller usually have completely distinct methods and accessors!

If your model and view/controller are going to have some methods in
common, and you want this ‘helper’ to take advantage of them, then
you’ve got to go the module include route others have said.

Jonathan

Max W. wrote:

Does anyone know where i can put a helper method so it can be accessed
from ANYWHERE? ie from a model, a controller or a view?

Currently i’ve got model helper methods in lib/model_extensions.rb,
controller helper methods in application.rb and view helper methods in
application_helper.rb.
But there’s lots of things (usually text helpers) that i want to be
available to anything.

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

thanks
max

On Sep 20, 2007, at 12:30 PM, Max W. wrote:

I made a file called general_helpers.rb and put it in my lib

include ‘general_helpers’

include GeneralHelpers

<%= hello %>

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 B. http://agileconsultingllc.com
[email protected]

Rob B. wrote:

On Sep 20, 2007, at 12:30 PM, Max W. wrote:

I made a file called general_helpers.rb and put it in my lib

include ‘general_helpers’

include GeneralHelpers

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

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

Thanks rob - this is still crashing with an “unknown method” problem
though. I’m not doing anything in environment.rb, just saying, in my
view page:

include GeneralHelpers

<%= hello %>

I’ve noticed that sometimes the stack trace stops at the .rhtml file,
when the error was really in the method being called. Is there something
in the ‘hello’ method definition that could be raising the method not
found exception?

ruby-debug may be helpful for debugging. It works well with Rails.

Max W. wrote:

Rob B. wrote:

On Sep 20, 2007, at 12:30 PM, Max W. wrote:

I made a file called general_helpers.rb and put it in my lib

include ‘general_helpers’

include GeneralHelpers

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

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

Thanks rob - this is still crashing with an “unknown method” problem
though. I’m not doing anything in environment.rb, just saying, in my
view page:

include GeneralHelpers

<%= hello %>

Bob S. wrote:

On 9/20/07, Max W. [email protected] wrote:

include ‘general_helpers’

wrong syntax. include takes a module name.

<%= hello %>

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.

ahh, i didn’t realise it had to be in ApplicationHelper…ok, that
works.

Putting the same line in application.rb allows me to use the method in
controllers as well. So, 2 out of 3 so far! :slight_smile:

However, i also put the same line in model_extensions, which gives
methods that can be called by any model. But, when i try to call the
method inside one of my model’s methods i get an “unknown method
“hello”” error again.

Thanks for your help - do you know why i’m not picking it up in my
models?

On Sep 20, 2007, at 1:10 PM, Max W. wrote:

-Rob

Thanks rob - this is still crashing with an “unknown method” problem
though. I’m not doing anything in environment.rb, just saying, in my
view page:

include GeneralHelpers

<%= hello %>

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 B. http://agileconsultingllc.com
[email protected]

Max W. wrote:

Bob S. wrote:

However, i also put the same line in model_extensions, which gives
methods that can be called by any model. But, when i try to call the
method inside one of my model’s methods i get an “unknown method
“hello”” error again.

Thanks for your help - do you know why i’m not picking it up in my
models?
Btw, this might be to do with the fact that ModelExtensions is itself a
module - can you include a module in another module?

On 9/20/07, Max W. [email protected] 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

On 9/20/07, Max W. [email protected] wrote:

include ‘general_helpers’

wrong syntax. include takes a module name.

<%= hello %>

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.

Bob S. wrote:

On 9/20/07, Max W. [email protected] 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…

Bob S. wrote:

On 9/20/07, Max W. [email protected] wrote:

work (put it in your general_helpers.rb)
I have a file in the lib folder called “general_helpers.rb” with
include GeneralHelpers
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’

Thanks again Bob, but still no joy. Even the console doesn’t work - it
seems to load the GeneralHelpers class ok, but give an “unrecognised
method” error for hello. I must have done something wrong somewhere, i
have no ide what though.

On 9/20/07, Max W. [email protected] wrote:

work (put it in your general_helpers.rb)
I have a file in the lib folder called “general_helpers.rb” with
include GeneralHelpers
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’