Forum: Ruby on Rails [AJAX] related drop-down list question

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Harm de Laat (Guest)
on 2006-01-10 10:56
(Received via mailing list)
Hi all,

I followed the example on: 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 = "<select id='album_id' name='album_id'>"
@html += "<option value=''>No Album</option>"
@albums.each do |@album|
   @html += "<option value='#{}'>#{@album.album_name}</option>"
@html += "</select>"

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
Alex Y. (Guest)
on 2006-01-10 11:27
(Received via mailing list)
Harm de Laat wrote:
> @albums.each do |@album|
>    @html += "<option value='#{
> <>}'>#{@album.album_name}</option>"...
> end
> @html += "</select>"
In the controller:

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

In the view:

<select id='album_id' name='album_id'>
<option value=''>No album</option>
<% for album in  @albums -%>
<option value='<%= %>'><%= album.album_name %></option>
<% end -%>

Or alternatively:

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

which should do the same thing, with the exception that the <select>
tag's id and name will be slightly different.
This topic is locked and can not be replied to.