1. enhacements to base classes
(like Numeric.format_integer or Time.format_shor_date)
I created files in \lib (like \lib\format.rb) and added
require "format"
in \app\controllers\application.rb
2. general helpers I want to be available to every view
I created a file in \lib (like \lib\debug_helper_module.rb), with a module definition (like "module DebugHelper")
then in \app\helpers\application_helper.rb
require "debug_helper_modules"
# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
include DebugHelper
end
but it didn't work
Well, I'm just giving my first steps with ruby, but it would be great to have a list telling where to put everything, in order to have a clean and tidy environment.
1. In the lib folder is the best place. If you are doing more than small modifications you might consider wrapping it up as a Rails plugin instead.
2. Either in application_helper.rb or in their own separate files (in the lib folder) which you can then mixin to ApplicationHelper. Not sure why that isn't working for you, I've never had problems with it. I may be missing something obvious that somebody else could point out.
def data
unless @data
@data = @dbman.restore
end
@data
end
end
----------
file: \app\controllers\application.rb
# Filters added to this controller will be run for all controllers in the application.
# Likewise, all the methods added will be available for all controllers.
class ApplicationController < ActionController::Base
end
#format methods for class Numeric String Time
require "format"
#session as a hash for class CGI::Session
require "session"
---------------------
2. Making a helper available to each and every controller
==========================================
# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
include DebugHelper
end
-------------------------------
Well, that's it.
The difference is that in the first place, I just need to add a couple of methos to existing classes, so with just "requiring" them from application.rb is enought.
In the second place, I want to "include" methods inside a module that will be mixed-in every controller. That is, I want to mix a module inside a module that will be mixed (clear as mud, I guess)
Bookmarks