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 = "<select id=''album_id'' name=''album_id''>" @html += "<option value=''''>No Album</option>" @albums.each do |@album| @html += "<option value=''#{@album.id}''>#{@album.album_name}</option>" end @html += "</select>" 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060110/1256218f/attachment.html
Harm de Laat wrote:> 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 = "<select id=''album_id'' name=''album_id''>" > @html += "<option value=''''>No Album</option>" > @albums.each do |@album| > @html += "<option value=''#{@album.id > <http://album.id>}''>#{@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.id %>''><%= album.album_name %></option> <% 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 <select> tag''s id and name will be slightly different. -- Alex
Reasonably Related Threads
- need to transfer data from old php like db to rails style db
- How to set a relationship with a value from a selection list
- bug in bubbleshare's Active Record extensions?
- form.select wrong number of arguments (0 for 1)
- Inserting data from 1 table to another by using find_by_sql