[AJAX] related drop-down list question


#1

Hi all,

I followed the example on: http://www.roryhansen.ca/?p=9 for creating
related drop-down lists.

However, this code actually creates HTML in the controller component:

@albums = Album.find_all_by_artist_id(@params[“artist_id”])
@html = “”
@html += “No Album”
@albums.each do |@album|
@html += “#{@album.album_name}”
end
@html += “”

I would think this is considered ‘bad practice’, because this is the job
of
the view.

How could one rewrite this and get a cleaner controller code?

Regards,

Harm de Laat


#2

Harm de Laat wrote:

@albums.each do |@album|
@html += “#{@album.album_name}”
end
@html += “”
In the controller:

@albums = Album.find_all_by_artist(@params[‘artist_id’])

In the view:

No album <% for album in @albums -%> <%= album.album_name %> <% end -%>

Or alternatively:

<%= select(‘album’, ‘id’,
[[’’, ‘No album’]] +
@albums.collect{|album| [album.id, album.name]}) -%>

which should do the same thing, with the exception that the
tag’s id and name will be slightly different.