I have a form on index.html.erb (User views):
<%= form_tag( '', :method => :get ) do %>
<% @company = Position.all.map { |p| [ p.company, p.company ] } %>
<%= select_tag "company", options_for_select((@company), params[:position_id]), { :include_blank => true, :reject_if => @company.blank? } %>
<% @industry = Position.all.map { |p| [ p.industry, p.industry ] } %>
<%= select_tag "industry", options_for_select((@industry), params[:position_id]), { :include_blank => true, :reject_if => @industry.blank? } %>
<%= submit_tag 'Filter', class: 'btn btn-large btn-primary' %>
<% end %>
and a controller (User controller):
def index
if params[:company] && params[:industry]
@users = User.companies(params[:company]).industries(params[:industry])
elsif params[:company]
@users = User.companies(params[:company])
elsif params[:industry]
@users = User.companies(params[:industry])
else
@users = User.all
end
end
A User has many companies and industries through positions:
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation, :first_name, :last_name, :position_ids
has_many :positions
has_many :companies, :through => :positions
has_many :industries, :through => :positions
has_one :current_position, :class_name => "Position", :conditions => { :is_current => true }
scope :companies, lambda { |*company| {:include => :positions, :conditions => ["positions.company = ?", company]} }
scope :industries, lambda { |*industry| {:include => :positions, :conditions => ["positions.industry = ?", industry]} }
end
Despite the if statement in my user controller, I cannot get my view to ignore blank entries in either the company or industry field. For instance, a blank company and "Internet" industry filter returns this url:
...users?utf8=%E2%9C%93&company=&industry=Internet&commit=Filter
how do I modify my code to ignore a blank company field so that the url excludes 'company=&' entirely? In this case, I get the returned results that I want:
...users?utf8=%E2%9C%93&industry=Internet&commit=Filter
thanks!