How to use a helper which is not inside app/helpers/


#1

Hi,

I wish to know it is possible to use a helper which is
not inside the folder ‘app/helpers’, if it is
possbile, what should I do?

Thanks you very much!!!

Saiho


Yahoo! DSL ? Something to write home about.
Just $16.99/mo. or less.
dsl.yahoo.com


#2

Edit config/environment.rb:

Uncomment config.load_path and add the path that contains your helper.
E.g., if your helper is in dir src/rails/helpers and your rails app is
called plan and is located in src/plan, add this to environment.rb:

config.load_paths += %W( #{RAILS_ROOT}/extras
#{RAILS_ROOT}/…/rails/helpers )

Create a file in src/rails/helpers called (for example)
formatting_helper.rb:

module FormattingHelper

def format_time(time, format_str = “%m/%d/%Y
%I:%M:%S %p”)
time.strftime(format_str)
end
end

Now in your controller add

helper :formatting

In your views you can access format_time. E.g.:

Started on: <%= format_time @task.created_at %>

Hope this helps.

Stephen V.


#3

Hi,

thanks you very much for the information, however, I
have tried the cmd “config.load_path”, but it doesn’t
work, I have felling that rails doesn’t not recognized
the path that I added.

actually, the extra helper modules is outside of the
application rails file structure. (I’m using linux)

the extra helpers is stored at /opt/web/extra/helpers/
so I added the following line at the
config/environment.rb

config.load_paths += %W("/opt/web/extra/helpers/")
and in my controller I added

helper :extra (which the module name is extraHelper,
and the filename is extra_helper.rb)

rails seems to ignore that. when I try to shoe the
webpage, I got error that rails is not able to locate
the extra_helper in /app/helpers/

How can I tell rails that it should look in
/opt/web/extra/helpers/ instead of /app/helpers/?

Thanks you very much

Saiho

— Stephen V. removed_email_address@domain.invalid wrote:

end

Started on: <%= format_time @task.created_at which is > __________________________________________ > _______________________________________________ Rails mailing list removed_email_address@domain.invalid http://lists.rubyonrails.org/mailman/listinfo/rails


Yahoo! DSL ? Something to write home about.
Just $16.99/mo. or less.
dsl.yahoo.com