lib/mir_extensions.rb
def select_tag_for_filter(model, nvpairs, params)
      return unless model && nvpairs && ! nvpairs.empty?
      options = { :query => params[:query] }
      _url = url_for(eval("#{model}_url(options)"))
      _html = %{<label for="show">Show:</label><br />}
      _html << %{<select name="show" id="show" onchange="window.location='#{_url}' + '?show=' + this.value">}
      nvpairs.each do |pair|
        _html << %{<option value="#{pair[:scope]}"}
        if params[:show] == pair[:scope] || ((params[:show].nil? || params[:show].empty?) && pair[:scope] == "all")
          _html << %{ selected="selected"}
        end
        _html << %{>#{pair[:label]}}
        _html << %{</option>}
      end
      _html << %{</select>}
    end
def select_tag_for_filter(model, nvpairs, params)
      return unless model && nvpairs && ! nvpairs.empty?
      options = { :query => params[:query] }
      _url = url_for(eval("#{model}_url(options)"))
      _html = %{<label for="show">Show:</label><br />}
      _html << %{<select name="show" id="show" onchange="window.location='#{_url}' + '?show=' + this.value">}
      nvpairs.each do |pair|
        _html << %{<option value="#{pair[:scope]}"}
        if params[:show] == pair[:scope] || ((params[:show].nil? || params[:show].empty?) && pair[:scope] == "all")
          _html << %{ selected="selected"}
        end
        _html << %{>#{pair[:label]}}
        _html << %{</option>}
      end
      _html << %{</select>}
    end
def select_tag_for_filter ( model , nvpairs , params ) return unless model && nvpairs && ! nvpairs . empty? options = { :query => params [ :query ] } _url = url_for ( eval ( "#{model}_url(options)" ) ) _html = %{<label for="show">Show:</label><br />} _html << %{<select name="show" id="show" onchange="window.location='#{_url}' + '?show=' + this.value">} nvpairs . each do | pair | _html << %{<option value="#{pair[:scope]}"} if params [ :show ] == pair [ :scope ] || ( ( params [ :show ] . nil? || params [ :show ] . empty? ) && pair [ :scope ] == "all" ) _html << %{ selected="selected"} end _html << %{>#{pair[:label]}} _html << %{</option>} end _html << %{</select>} end
Use on index pages to create dropdown list of filtering criteria.
 Populate the filter list using a constant in the model corresponding to named scopes.

 Usage:

 - item.rb:

     scope :active,   :conditions => { :is_active => true }
     scope :inactive, :conditions => { :is_active => false }

     FILTERS = [
       {:scope => "all",       :label => "All"},
       {:scope => "active",    :label => "Active Only"},
       {:scope => "inactive",  :label => "Inactive Only"}
     ]

 - items/index.html.erb:

     <%= select_tag_for_filter("items", @filters, params) -%>

 - items_controller.rb:

     def index
       @filters = Item::FILTERS
       if params[:show] && params[:show] != "all" && @filters.collect{|f| f[:scope]}.include?(params[:show])
         @items = eval("@items.#{params[:show]}.order_by(params[:by], params[:dir])")
       else
         @items = @items.order_by(params[:by], params[:dir])
       end
       ...
     end
Use on index pages to create dropdown list of filtering criteria . Populate the filter list using a constant in the model corresponding to named scopes .