-1

字符串值数组未填充到选项列表中。我不确定我可能哪里出错了。这是我坚持的代码。

<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>
4

1 回答 1

1

In Ruby instead of

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

you can use Array#each:

ary.each do |item|
  item
end

This is how I would do it in ERB (more or less):

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

Output:

<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>

Explanation:

  • %w(Submitted Replied Answered Assigned Started Closed Canceled) creates the array
  • <% ary.each do |status| %> ... <% end %> is a loop that's executed for each element, the variable status refers to the current element within the loop
  • <option value="<%= status %>"><%= status %></option> is the output for each element, <%= status %> is replaced with the corresponding element (Submitted, Replied, ...)
于 2013-08-30T10:25:03.750 回答