[Rails] Where to put code for layouts?
Lars Hoss
woeye at highteq.net
Wed Jan 5 21:24:35 GMT 2005
Hi all!
I want to show a list of all available categories on various pages.
Thus I put the HTML-code into a layout. In order to get the list of
categories I have to talk to the database using AR. The question now
is: where to put that code? My first idea was to put the code into
ApplicationHelper:
module ApplicationHelper
def categories
return Category.find_all
end
end
Since I need the model Category I though "well, there's this new cool
dependency feature" and wrote:
module ApplicationHelper
module :category
def categories
return Category.find_all
end
end
This, however, only works for controllers and not helpers. Therefore
the correct way is:
require 'Category'
module ApplicationHelper
def categories
return Category.find_all
end
end
Personally I find this a little bit confusing because controllers use
the "model :" approach whereas helpers must use the old-style "require"
approach. If you know what to do it's ok. Newbies on the other hand
might be confused a little bit first (when to use what)?
Yours,
Lars
--
"Stil ist die Fähigkeit, komplizierte Dinge einfach zu sagen - nicht
umgekehrt." -- Cocteau, Jean
More information about the Rails
mailing list