Topic: Getting Drop Down Value in Entry screen

Dear Sir,

Hello ! I am new to RoR.
In the following form, while entering data, on selection of value in a static drop down, property type (HDB, Private, Landed House etc), I want to enable/disable certain below fields.

Please guide me how to get the value of a select col while user does the entry and skip to next field on selecting the value and disable selected fields - please give example of disable for text_field, select, collection_select com and datetime_select fields.

The listing for new.html containing these fields are as below. The selection to check on Property Type field below and disable certain fields based on its value.

new.rhtml

<% form_tag :action => 'create' do %>
   <table border="2" align="center" bgcolor="#edf4fa" bordercolor="#0033CC" width="600">
        <tr bgcolor="#4171b1">
                <td width="600" height="40" colspan="4"  align="center" style="font-weight:bold; font-size:14px; color: #ffffff;
                border:none">Ad Entry </td>
        </tr>
            <tr>
              <td height="25" colspan="2" align="left" valign="top" style="font-weight:bold; font-style:italic; font-size:12px;
              color: #FF0000; border:none">
                    <%= error_message_on :adv, :prop_address %>
            </td>
            </tr>
               <tr>
               <td width="140" align="right" valign="middle" style="border:none"><span class="style28">Transaction Type </span>                         </td>
               <td width="442" colspan="3" style="border:none"><%= select ( 'adv', 'tran_type', {'Sale' => 'S', 'Rent' => 'R'} ) %></td>
            </tr>
            <tr>
               <td width="140" height="10" style="border:none"></td>
               <td width="442" height="10" colspan="3" style="border:none"></td>
            </tr>
            <tr>
               <td width="140" align="right" valign="middle" style="border:none"><span class="style28">Create Date    </span></td>
               <td width="442" colspan="3" align="left" valign="middle" style="border:none">
              <%= datetime_select 'adv', 'create_date'  %>              
              </td>
            </tr>           
            <tr>
               <td width="140" height="10" style="border:none"></td>
               <td width="442" height="10" colspan="3" style="border:none"></td>
            </tr>
            <tr>
               <td width="140" align="right" valign="middle" style="border:none"><span class="style28">Property Type     </span>                                     </td>
               <td width="169" style="border:none"><%= collection_select ( :adv, :prop_type, @proptypes, :id, :name  ) %></td>
             <td width="123" align="right" style="border:none">Property Sub Type </td>
             <td width="170" style="border:none"><%= collection_select ( :adv, :prop_subtype, @prsubtypes, :id, :name  ) %></td>
            </tr>
            <tr>
               <td width="140" height="8" style="border:none"></td>
               <td width="442" height="8" colspan="3" style="border:none"></td>
            </tr>
            <tr>
               <td width="140" align="right" valign="middle" style="border:none"><span class="style28">Surface area    </span></td>
               <td width="169" style="border:none"><%= text_field 'adv', 'surface_area', {"size" => 10}  %></td>
             <td width="123" align="right" style="border:none">Age</td>
             <td width="170" style="border:none"><%= select ( 'adv', 'age', {'5 years or less' => '1', '6 to 10 years' => '2', '11 to 15 years' => '3', '16 to 20 years' => '4', 'More than 20 years' => '5'} ) %></td>
            </tr>
            <tr>
               <td width="140" height="8" style="border:none"></td>
               <td width="442" height="8" colspan="3" style="border:none"></td>
            </tr>            
            <tr>
               <td width="140" align="right" valign="middle" style="border:none"><span class="style28">Status   </span></td>
               <td width="442" colspan="3" style="border:none"><%= select ( 'adv', 'status', {'Unapproved' => '1', 'Active' => '2', 'Archived' => '3'} ) %></td>
            </tr>
            <tr>
               <td width="140" height="8" style="border:none"></td>
               <td width="442" height="8" colspan="3" style="border:none"></td>
            </tr>
            <tr>
               <td width="140" align="right" valign="middle" style="border:none"><span class="style28">Ad Type  </span></td>
               <td width="442" colspan="3" style="border:none"><%= select ( 'adv', 'ad_type', {'HDB' => 'H', 'Pvt. Apartments' => 'P', 'Landed House' => 'L', 'Rental Info' => 'R'} ) %></td>
            </tr>           
            <tr>
               <td width="140" height="8" style="border:none"></td>
               <td width="442" height="8" colspan="3" style="border:none"></td>
            </tr>
            <tr>
               <td width="140" align="right" valign="middle" style="border:none"><span class="style28">Summary    </span></td>
               <td width="442" colspan="3" style="border:none"><%= text_field 'adv', 'summary', {"size" => 60}  %></td>
            </tr>
           
            <tr>
               <td width="140" height="8" style="border:none"></td>
               <td width="442" height="8" colspan="3" style="border:none"></td>
            </tr>
            <tr>
               <td width="140" align="right" valign="middle" style="border:none"><span class="style28">Detailed Description   
             </span></td>
               <td width="442" colspan="3" style="border:none"><%= text_area 'adv', 'detail_desc', {"cols" => 50, "rows" => 4} %></td>
            </tr>
            <tr>
               <td width="140" height="8" style="border:none"></td>
               <td width="442" height="8" colspan="3" style="border:none"></td>
            </tr>
            <tr>
               <td width="140" align="right" valign="middle" style="border:none"><span class="style28">Price   </span></td>
               <td width="442" colspan="3" style="border:none"><%= text_field 'adv', 'price', {"size" => 10}  %></td>                         </td>
            </tr>
           
            <tr>
               <td width="140" height="8" style="border:none"></td>
               <td width="442" height="8" colspan="3" style="border:none"></td>
            </tr>
            <tr>
              <td width="140" align="right" valign="middle" style="border:none"><span class="style28">No of Bed Rooms    </span>                          </td>
              <td width="169" align="left" valign="middle" style="border:none"><%= text_field 'adv', 'tot_bedroom', {"size" => 3}  %></td>
              <td width="123" align="right" valign="middle" style="border:none"><span class="style28">No of Bath Rooms</span></td>
              <td width="170" align="left" valign="middle" style="border:none"><%= text_field 'adv', 'tot_bathroom', {"size" => 3}  %></td>
            </tr>           
            <tr>
               <td width="140" height="8" style="border:none"></td>
               <td width="442" height="8" colspan="3" style="border:none"></td>
            </tr>
            <tr>
              <td width="140" align="right" valign="middle" style="border:none"><span class="style28">Address   
               </span></td>
              <td width="442" colspan="3" align="left" valign="middle" style="border:none">
              <%= text_area 'adv', 'prop_address', {"cols" => 50, "rows" => 4} %></td>
            </tr>
            <tr>
               <td width="140" height="8" style="border:none"></td>
               <td width="442" height="8" colspan="3" style="border:none"></td>
            </tr>
            <tr>
              <td width="140"  align="right" valign="middle" style="border:none"><span class="style28">City    </span></td>
              <td width="442" colspan="3" align="left" valign="middle" style="border:none">
              <%= text_field 'adv', 'city_name', {"size" => 30}  %></td>
            </tr>
            <tr>
               <td width="140" height="8" style="border:none"></td>
               <td width="442" height="8" colspan="3" style="border:none"></td>
            </tr>
            <tr>
              <td width="140" align="right" valign="middle" style="border:none"><span class="style28">State    </span></td>
              <td width="169" align="left" valign="middle" style="border:none">
              <%= text_field 'adv', 'state_name', {"size" => 30}  %></td>
              <td width="123" align="right" valign="middle" style="border:none"><span class="style28">Zip  </span></td>
              <td width="170" align="left" valign="middle" style="border:none"><%= text_field 'adv', 'zip', {"size" => 15}  %></td>
            </tr>
            <tr>
               <td width="140" height="8" style="border:none"></td>
               <td width="442" height="8" colspan="3" style="border:none"></td>
            </tr>
            <tr>
              <td width="140" align="right" valign="middle" style="border:none"><span class="style28">Contact Details     </span>               </td>
              <td width="169" align="left" valign="middle" style="border:none"><%= select ( 'adv', 'show_contact', {'Show' => 'Y', 'Hide' => 'N'} ) %></td>
              <td width="123" align="right" valign="middle" style="border:none"><span class="style28">Ad Type    </span></td>
              <td width="170" align="left" valign="middle" style="border:none"><%= select ( 'adv', 'adv_stdpm', {'Premium' => 'P', 'Standard' => 'S'} ) %>
              </td>
            </tr>           
            <tr>
               <td width="140" height="8" style="border:none"></td>
               <td width="442" height="8" colspan="3" style="border:none"></td>
            </tr>       
   
            <tr>
                <td width="140" height="81" style="border:none">&nbsp;</td>
              <td width="442" align="center" style="border:none"><%= submit_tag "Create" %> </td>
               </tr>
   </table>
<% end %>

The controller listing is as below :
class AdvmanageController < ApplicationController
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @adv_pages, @advs = paginate :advs, :per_page => 10
  end

  def show
    @adv = Adv.find(params[:id])
  end

  def new
    @adv = Adv.new
    @proptypes = Proptype.find(:all)
    @prsubtypes = Prsubtype.find(:all)
  end

  def create
    # @t = Adv.find(params[:user_id])
    @adv = Adv.new(params[:adv])
    @adv[:user_id]= 1
    @proptypes = Proptype.find(:all)
    @prsubtypes = Prsubtype.find(:all)
    if @adv.save
      flash[:notice] = 'Adv was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

  def edit
    @adv = Adv.find(params[:id])
    @proptypes = Proptype.find(:all)
    @prsubtypes = Prsubtype.find(:all)
  end

  def update
    @adv = Adv.find(params[:id])
    @proptypes = Proptype.find(:all)
    @prsubtypes = Prsubtype.find(:all)
    if @adv.update_attributes(params[:adv])
      flash[:notice] = 'Adv was successfully updated.'
      redirect_to :action => 'list'
      # redirect_to :action => 'show', :id => @adv
    else
      render :action => 'edit'
    end
  end

  def destroy   
    # Deleting from Add on tables
    @advs_id = params[:id]
    if (AdvOther.find(:first, :conditions => [ "adv_id = ?", @advs_id]) != nil )
        AdvOther.find(:first, :conditions => [ "adv_id = ?", @advs_id]).destroy
      end
    if (AdvFile.find(:first, :conditions => [ "adv_id = ?", @advs_id]) != nil)
      AdvFile.find(:all, :conditions => [ "adv_id = ?", @advs_id]).destroy
    end   
    # Deleting from Main Table
    Adv.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
end


Please guide me soon !

Thanks & Regards,

Naimesh Trivedi
nbtrivedi@hotmail.com

Last edited by naimesh (2007-08-14 03:57:00)