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?
Hi max.
I tried to understand you 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
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.
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?
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.
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.
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!
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?
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).
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?
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:
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.
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: