0

我的 Rails 视图 (index.html.erb) 中有多个下拉框。代码如下所示:

......
......
<td>
<% @builds.each_with_index do |row,index|
    if row2[0].to_s == row_s[0] %> 
      (When this condition is true I want to set the value of the dropdown list)
          (The value could be PASS, FAIL or PENDING which comes from the database as row_s[7])

       <form id=<%= "build_status_form#{index}" %>>
           <select name="condition" 
             id=<%= "build_status#{index}" %>  onchange="this.form.submit()">
                   <option value="PENDING">PENDING</option>
                   <option value="PASS">PASS</option>
                   <option value="FAIL">FAIL</option>
           </select>
       </form>

    <% end %>
<% end %>
</td>

如何动态设置每个下拉列表的默认值?

4

3 回答 3

2

这可以使用 rails 助手在一行中完成

<form id=<%= "build_status_form#{index}" %>>
  <%= select_tag :condition, options_for_select(%w[PENDING PASS FAIL], row_s[7]), id: "build_status#{index}", onchange: 'this.form.submit()' %>
</form>
于 2013-03-14T05:49:19.030 回答
1

假设如果要设置默认值PASS使用selected

  <form id=<%= "build_status_form#{index}" %>>
       <select name="condition" 
         id=<%= "build_status#{index}" %>  onchange="this.form.submit()">
               <option value="PENDING">PENDING</option>
               <option value="PASS" selected="selected">PASS</option
               <option value="FAIL">FAIL</option>
       </select>
   </form>
于 2013-03-14T05:29:55.513 回答
1
<form id=<%= "build_status_form#{index}" %>>
           <select name="condition" 
             id=<%= "build_status#{index}" %>  onchange="this.form.submit()">

                   if(your value is true)
                   {
                       if value is pending then 
                         <option selected="selected" value="PENDING">PENDING</option>
                       else
                        <option  value="PENDING">PENDING</option>

                       like this you can write
                  } 
           </select>
       </form>

我在这里像 javascript 一样写。你可以根据需要在 ralis 中写。

于 2013-03-14T05:30:11.290 回答