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| %>
            <%= label_tag 'area', %>

            <div class="tenant">
              <%= select_tag "tenant_cases[]", options_for_select( {|sc| [,]}),{} %>

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

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

I have two tables in DB named Tenants and Areas

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

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.


Re: On Form Load combobox item to be selected

This should work:

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