[AJAX] related drop-down list question

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}”
@html += “”

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

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


Harm de Laat

Harm de Laat wrote:

@albums.each do |@album|
@html += “#{@album.album_name}”
@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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs