如何循环遍历属性和数组以在表单中创建表

ume*_*ezo 2 ruby-on-rails

我创建了调查,并有一堆命名的列的:movie_1通过:movie_5,我想通过做一个表,我的形式循环.这样做的最佳方法是什么?

到目前为止,我已经开始如下,但无法弄清楚如何格式化循环.

survey.rb模型

attr_accessible :movie_1, :movie_2, :movie_3, :movie_4, :movie_5

MOVIES = ["Rocky", "Wayne's World", "The Godfather", "Alien", "Toy Story"]
Run Code Online (Sandbox Code Playgroud)

new.html.erb调查表

<%= form_for @survey do |f|
  <table>
    <tr>
      <th>Movie Name</th>
      <th>Rating</th>
    </r>
    <% 5.times.each do |n| %>
      <tr>
        <th><%= f.label Survey::MOVIES[n] %></th> # how can I loop through the MOVIES array?
        <th><%= f.text_field :movie_n %></th> # how can I loop through the different columns?
      </tr>
    <% end %>
  </table>
<% end %>
Run Code Online (Sandbox Code Playgroud)

min*_*ank 5

在这种情况下,您可能需要考虑制作另一个Movie属于的模型,Survey然后使用嵌套表单来显示所有电影/更新它们.

这里有一些关于嵌套表单的好的railscast.