未在ruby中的选项列表中填充的字符串值数组

时间:2013-08-30 10:22:25

标签: ruby arrays erb html-select

字符串值数组未在选项列表中填充。我不知道我哪里出错了。这是我坚持的代码。

<html>
Status : <select id = 'status_update'>
             <%=
 array = ["Submitted", "Replied", "Answered", "Assigned", "Started", "Closed","Reopened",  "Canceled"]
          status = '';
           for index in 0 .. (array.size-1)
        status << "<option value = '#{puts (eval(array[index].inspect))}'>'#{(array[index].inspect)}'</option>"
                  end
              %>
  </select>
</html>

1 个答案:

答案 0 :(得分:1)

在Ruby而不是

for i in 0...ary.size do
  ary[i]
end

您可以使用Array#each

ary.each do |item|
  item
end

这就是我在ERB(或多或少)中的表现:

<% %w(Submitted Replied Answered Assigned Started Closed Canceled).each do |status| %>
  <option value="<%= status %>"><%= status %></option>
<% end %>

输出:

<option value="Submitted">Submitted</option>
<option value="Replied">Replied</option>
<option value="Answered">Answered</option>
<option value="Assigned">Assigned</option>
<option value="Started">Started</option>
<option value="Closed">Closed</option>
<option value="Canceled">Canceled</option>

说明:

  • %w(Submitted Replied Answered Assigned Started Closed Canceled)创建数组
  • <% ary.each do |status| %> ... <% end %>是为每个元素执行的循环,变量status是指循环中的当前元素
  • <option value="<%= status %>"><%= status %></option>是每个元素的输出,<%= status %>替换为相应的元素(SubmittedReplied,...)