Both Cities and Positions are associated to Jobs through a has_many :through relationship.
On the Jobs#Index page, I have a form, which when submitted allows a user to filter jobs based on a combination of city and positions.
I got this to work, but know the code can be a lot better.
This is my Jobs#Index controller:
def index
@cities = City.all
@positions = Position.all
@city = City.find(params[:city_id]) if params[:city_id] && params[:city_id] != "0"
@position = Position.find(params[:position_id]) if (params[:position_id] && params[:position_id] != "0")
@jobs = Job.all
@jobs = @jobs.includes(:cities).where(cities: { id: @city }) if @city
@jobs = @jobs.includes(:positions).where(positions: { id: @position }) if @position
@jobs = @jobs.paginate(page: params[:page], per_page: 5)
end
This is my Jobs#Index View:
- provide(:title, 'All Jobs')
.thinstripe_career
.darken
.container
.row
.span10.offset1
.jobs_header
.row
.span7
h2 All #{@city.name if @city} #{@position.name if @position} Jobs
.span3
== link_to "Sign up for Job Alerts!", "#", :class => "button"
- if current_user && current_user.admin?
== render 'shared/dashboard_header'
.container
.row
.span10.offset1
.job_search
== form_tag jobs_path, :method => 'get', :id => "jobs_filter" do
h2 I'd like jobs in
.filter_sect
== select_tag :city_id, content_tag(:option,'all cities...', :value=>"0") + options_from_collection_for_select(@cities, "id", "name", params[:city_id].to_i ), :class => "basic"
.filter_sect
== select_tag :position_id, content_tag(:option,'all roles...',:value=>"0") + options_from_collection_for_select(@positions, "id", "name", params[:position_id].to_i ), :class => "basic"
.filter_sect.filter_search
== submit_tag "Search", :class => "button search_button button_black", :name => nil
ul.jobs
- if @jobs.any?
== render @jobs
- else
li.non_founds
h2 No jobs founds.
p There are currently no jobs founds for these combinations. Sign up to requests
== will_paginate