title: getting the selected value from the drop down in rails
body: <pre><code>enter code here
  
  &lt;h1&gt;Add New Investment Opportunity&lt;/h1&gt;
  
  &lt;%= form_for Investopp.new do |f| %&gt;
  
  &lt;div class="field"&gt;
  &lt;%= f.label :state_id %&gt;&lt;br/&gt;
  &lt;%= f.collection_select  :state_id, Investopp.year_lookup(@state_ids), :label, :value, include_blank: true %&gt;
  &lt;/div&gt;
  
  &lt;div class="field"&gt;
  &lt;%= f.label :city_id, "city" %&gt;&lt;br /&gt;
  &lt;%= f.grouped_collection_select :city_id, Investopp.year_lookup(@state_ids), :cities, :value, :id,:name, include_blank: true %&gt;
  &lt;/div&gt;
  &lt;% end %&gt;
  &lt;%= link_to 'Back', investopps_path %&gt;
  &lt;%= link_to 'Search', investopps_browsedisplay_path %&gt;
  
  
  class Investopp &lt; ActiveRecord::Base
  attr_accessible :Address, :Buildingname, :Desiredinvrole, :Details,   :Prefferednoofinvestors, :Salesprice, :Weblisting, :photo, :user_id, :state_id, :city_id, :state, :city
  has_attached_file :photo, :styles =&gt; { :small =&gt; "200x200&gt;" }
  belongs_to :user
  validates :Buildingname, presence: true
  validates :Address, presence: true
  validates :Desiredinvrole, presence: true
  validates :Weblisting, presence: true
  validates :Details, presence: true
  has_many :states
  has_many :cities
  def find_state(id)
    if !id  || id==0
        id=1
    end
  
    @states= State.find(id)
    @states.name
  
  end
  def find_city(id)
    if !id  || id==0
        id=1
    end
  
    @cities= City.find(id)
    @cities.name
  
  end
  
  def self.year_lookup(state_ids)
  
  #create an emptycollection to hold the LabelValue Objects
  years = []
  
  state_ids.each do |yr| y = LabelValue.new()
  y.label = yr
  y.value = State.find_by_id(yr).name
  years.push(y)
  end
  
  years
  
  end
  
  def self.state_lookup(state_ids)
  years = []
  
  state_ids.each do |yr| y = State.new()
   y= State.find_by_id(yr)
  years.push(y)
  end
  
  years
  
  end 
  
  end
  
  class LabelValue
  # name the accessors. Label and Value
  attr_accessor :label, :value
  
  
  def cities
  cityids=[]
  state_cities=[]
  
  investopps=Investopp.find(:all)
  investopps.each do |i|
  puts i.city_id
  cityids &lt;&lt;i.city_id
  end
  cityids.uniq!
  
  states=State.find_by_id(label)
  cityids.each do |c|
  if states.cities.find_by_id(c)
  state_cities&lt;&lt; states.cities.find_by_id(c)  
  end
  
  end
  
  state_cities
  end
  
  end
  
  
  class InvestoppsController &lt; ApplicationController
  # GET /investopps
  # GET /investopps.json
  def index
  @investopps = Investopp.where(:user_id =&gt; current_user.id)
  
  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @investopps }
  end
  end
  </code></pre>
  
  <p># GET /investopps/1
     # GET /investopps/1.json
     def show</p>
  
  <pre><code>@investopp = current_user.investopps.find(params[:id])
  
  respond_to do |format|
    format.html # show.html.erb
    format.json { render json: @investopp }
  end
  end
  </code></pre>
  
  <p># GET /investopps/new
    # GET /investopps/new.json
    def new
      @investopp = Investopp.new</p>
  
  <pre><code>respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @investopp }
  end
  end
  </code></pre>
  
  <p># GET /investopps/1/edit
      def edit
      @investopp = Investopp.find(params[:id])
      end</p>
  
  <p># POST /investopps
    # POST /investopps.json
      def create</p>
  
  <pre><code>#params[:investopp][:state_id]= "gopi"
  #params[:investopp][:city_id]= "33" 
  @investopp = current_user.investopps.build(params[:investopp])
  
  respond_to do |format|
    if @investopp.save
      format.html { redirect_to @investopp, notice: 'Investopp was successfully created.'    }
      format.json { render json: @investopp, status: :created, location: @investopp }
    else
      format.html { render action: "new" }
      format.json { render json: @investopp.errors, status: :unprocessable_entity }
    end
  end
  </code></pre>
  
  <p>end</p>
  
  <p># PUT /investopps/1
    # PUT /investopps/1.json
    def update
      @investopp = Investopp.find(params[:id])</p>
  
  <pre><code>respond_to do |format|
    if @investopp.update_attributes(params[:investopp])
      format.html { redirect_to @investopp, notice: 'Investopp was successfully updated.'   }
      format.json { head :no_content }
    else
      format.html { render action: "edit" }
      format.json { render json: @investopp.errors, status: :unprocessable_entity }
    end
  end
  </code></pre>
  
  <p>end</p>
  
  <p># DELETE /investopps/1
    # DELETE /investopps/1.json
    def destroy
      @investopp = Investopp.find(params[:id])
      @investopp.destroy</p>
  
  <pre><code>respond_to do |format|
    format.html { redirect_to investopps_url }
    format.json { head :no_content }
  end
  </code></pre>
  
  <p>end</p>
  
  <pre><code>def lis
  @state_names=[]
  @state_ids=[]
  @city_ids= []
  @city_names=[]
  @investopp = Investopp.find(:all)
  @investopp.each do |item|
    @state_names&lt;&lt; State.find_by_id(item.state_id).name
    @state_ids&lt;&lt; item.state_id
    @city_names&lt;&lt; City.find_by_id(item.city_id).name
    @city_ids &lt;&lt; item.city_id
  end
  puts  @state_ids.uniq!{|i| i}
  puts  @city_ids.uniq!{|i| i}
  
  puts "gopi"
  respond_to do |format|
    format.html { render "investopps/lis", :locals =&gt; { :state_ids =&gt; @state_ids, :city_ids =&gt; @city_ids, :investopps =&gt;  @investopp } }
    format.json { render json: @investopp }
  end
  </code></pre>
  
  <p>end</p>
  
  <p>end</p>