Topic: On Form Load combobox item to be selected

Hello all,

I have a form where i have numerous labels and Comboboxes based on the number of data in the DB. Below is the code for the same.

<%= form_tag({:action => :apply_area, :method => :get}) do |f| %>

   <% @facility.areas.each do |area| %>
      <td>
            <%= label_tag 'area', area.name %>
      </td>

       <td>
            <div class="tenant">
              <%= select_tag "tenant_cases[]", options_for_select(Tenant.order.map {|sc| [sc.name, sc.id]}),{} %>
            </div>
       </td>   

   <% end %>
<div class="actions">
    <%= submit_tag ("更新する") %>
</div>
<% end %>

At present the data gets populated, but my need is something different.

I have two tables in DB named Tenants and Areas

Tenants
-----------
id |  facility_id | name |
1          1          Tenant1
2          1          Tenant2

Areas
--------
id | facility_id | tenant_id | name |
1         1                 1          Area 1

if there is any tenant_id saved in the Areas table then the respective tenant name should be selected in the combobox
Eg for Area1 tenant_id is 1 so in the combbox Tenant1 should get selected on the form load.


Regards,
AB

Re: On Form Load combobox item to be selected

This should work:

<%= select_tag "tenant_cases[]", options_for_select(Tenant.order.map {|sc| [sc.name, sc.id]}, area.tenant_id),{} %>