Pi Horse
Pi Horse

Reputation: 2430

Setting default value of Dropdown in HTML - Special Case

I have multiple dropdown boxes in my Rails View (index.html.erb). The code looks like below:

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

How do I set the default value for each dropdown dynamically ?

Upvotes: 0

Views: 2044

Answers (3)

jvnill
jvnill

Reputation: 29599

This can be done in a single line using rails helpers

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

Upvotes: 2

PSR
PSR

Reputation: 40318

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

i write here like javascript.You can write this as you need in ralis .

Upvotes: 1

Devang Rathod
Devang Rathod

Reputation: 6736

Suppose if you want to set default value PASS use 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>

Upvotes: 1

Related Questions