I think this goes in app/helpers/application_helper.rb which should
already exist in your application.
I’m just guessing but could album_list be a model method? Then you can
do things like
@genre.album_list @artist.album_list
I think you would still want to factor out the code into a file in the
lib/ directory and then mix it into each model. I’ve asked about
locating the factored out code and didn’t get an answer that I liked.
You can see the thread here
Place it in ApplicationHelper, so all your views can access it. And
write it
this way:
def album_list( artist = nil, genre = nil )
return “” unless artist or genre # need to have at least one…
albums = artist ? artist.albums.sort : genre.albums.sort
list = albums.collect { |album| link = link_to album.title,
:controller => ‘albums’,
:action => ‘show’, :id => album.id
“
I think this goes in app/helpers/application_helper.rb which should
already exist in your application.
I’m just guessing but could album_list be a model method? Then you can
do things like
@genre.album_list @artist.album_list
You could go that way, but I’m not a huge fan of model methods returning
view
text, which is what his album_list helper method does…
I think you would still want to factor out the code into a file in the
lib/ directory and then mix it into each model. I’ve asked about
locating the factored out code and didn’t get an answer that I liked.
You can see the thread here
If you were adding the same method to multiple models, going the mixin
route
would make sense, but I’m not sure that’s what he’s doing here. Both of
his
helpers are working on a group of one model object, albums. He just has
different ways of getting the group.
On Jan 21, 2006, at 1:18 PM, Lieven De Keyzer wrote:
Tom M. wrote:
May I suggest a combination of the two?
DB access shortcut on the model, and a helper
to use the shortcut and format it?
Here’s an example from my code, to create a select list
for related objects. It’s not a complete example, as it
doesn’t eject HTML, but I think you’ll get the idea.
In the helper:
module SellersHelper
def get_units @user.get_units.map {|u| [u.name, u.id]}
end
end
In the view code:
<%= select ‘seller’,‘unit_id’, get_units %>
So, the @user object knows how to get the correct
objects (via the get_units method), and the helper
formats those objects for use in the view.