如何让2个循环在彼此之间显示信息

时间:2016-03-01 05:59:54

标签: ruby-on-rails ruby ruby-on-rails-4

我有两个不同哈希值生成的2个输入字段。

table_head = {"key1"=>"thead1", "key2"=>"thead2", "key3"=>"thead3", "key4"=>"thead4"}

table_content = {"1"=>"column1", "2"=>"column2", "3"=>"column3", "4"=>"column4"}

 <%= form_for([@category, @page], url: update_pages_path) do |f| %>

    <% @page.table_head.each_with_index do |(key, value), i| %>
      <%= text_field_tag ('header[' + i.to_s + ']'), value %> 
    <% end%>


    <% @page.table_content.each_with_index do |(key, value), i| %>
      <%= select_tag 'content[' + i.to_s + ']', options_for_select(@category_keys, value), { :multiple => true, :size => 3}  %>
    <% end%>


 <% end %>

这会使4个table_head输入和4个table_content选择。但我需要它们被排序,表头输入,然后表内容选择,然后表头输入,内容选择等。并非所有表头输入都会选择。

我可以为字段

执行此操作
 <% @page.table_head.each_with_index do |(key, value), i| %>
  <%= text_field_tag ('header[' + i.to_s + ']'), value %> 
  <%= select_tag 'content[' + i.to_s + ']', options_for_select(@category_keys), { :multiple => true, :size => 3}  %>
 <% end%>

但是,默认options_for_select value无法放入内容字段。

如何对领域,内容,头部,内容等进行排序..头部,头部,头部,头部,内容,内容等。

1 个答案:

答案 0 :(得分:0)

table_head = {"key1"=>"thead1", "key2"=>"thead2", "key3"=>"thead3", "key4"=>"thead4"}

table_content = {"1"=>"column1", "2"=>"column2", "3"=>"column3", "4"=>"column4"}

<%= form_for([@category, @page], url: update_pages_path) do |f| %>
  <% @page.table_head.zip(@page.table_content).each do |head, content| %>
    <% head_key, head_value = head %>
    <% content_key, content_value = content %>
    <%= text_field_tag ('header[' + head_key + ']'), head_value %> 
    <%= select_tag 'content[' + content_key + ']', options_for_select(@category_keys, content_value), { :multiple => true, :size => 3}  %>
  <%end%>
<% end %>
相关问题