I have an index page for Jobs. Both Cities and Positions has a has_many :through relationship with jobs.
On the Jobs Index Page, I have a small search form, which I would like to use to filter results by the combination of cities and Positions a user chooses.
This is how it looks so far
Jobs#Index
- 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, options_from_collection_for_select(@cities, "id", "name"), :class => "basic"
.filter_sect
== select_tag :position_id, options_from_collection_for_select(@positions, "id", "name"), :class => "basic"
.filter_sect.filter_search
== submit_tag "Search", :class => "button search_button button_black"
ul.jobs
== render @jobs
== will_paginate
Jobs#Index Controller
def index
@cities = City.all
@positions = Position.all
@city = City.find(params[:city_id]) if params[:city_id]
@position = Position.find(params[:position_id]) if params[:position_id]
@jobs = Job.paginate(page: params[:page])
end
I would like to @jobs to be be filtered buy @city or @position.
With the condition that if @city is not present, it only filters by @position, and vice versa. If both are not present, then it simply paginates all jobs. However, all the code I think of is quite long and ridden with if statements, so I think I'm going into the wrong path.
How would I go about filtering @jobs through @city and @position?