ruby on rails - How to loop through attributes and an array to create a table in a form -


i'm creating survey, , have bunch of columns named :movie_1 through :movie_5 loop through make table in form. best way so?

so far have started out below, can't figure out how format loop.

survey.rb model

attr_accessible :movie_1, :movie_2, :movie_3, :movie_4, :movie_5  movies = ["rocky", "wayne's world", "the godfather", "alien", "toy story"] 

new.html.erb survey form

<%= form_for @survey |f|   <table>     <tr>       <th>movie name</th>       <th>rating</th>     </r>     <% 5.times.each |n| %>       <tr>         <th><%= f.label survey::movies[n] %></th> # how can loop through movies array?         <th><%= f.text_field :movie_n %></th> # how can loop through different columns?       </tr>     <% end %>   </table> <% end %> 

this should work:

<% survey::movies.each_with_index |movie, index| %>   <tr>     <th><%= f.label movie %></th>     <th><%= f.text_field "movie_#{index+1}".to_sym %></th>   </tr> <% end %> 

however mind.blank right: it's bad design have movie_1, movie_2, etc fields in survey model. make more sense have movie model belongs survey.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -