I have a job listing project set up and I want to be able to filter by several filters. I want to be able to have sidebar which can filter by certain elements - :city, :jobtype, and :contracttype
Is there a straightforward way to create radio buttons that will display the options available to the user ie for :city a list of London, Manchester, Brighton etc which can be ticked to display those specific jobs?
I'm new to rails so having a hard time working out what I need to do, if anyone could explain what I need to do I'd really appreciate it!
My code is as follows:
index.html.erb -
<% @jobs.each do |job| %>
<div class="job">
<h2><%= job.position %></h2>
<p>Company: <%= job.company %></p>
<p>Salary: <%= job.salary %></p>
<p><a href="http://<%= job.companywebsite %>" target="_blank">Website: <%= job.companywebsite %></a></p>
<p><a href="http://www.twitter.com/<%= job.companytwitter %>" target="_blank">Twitter: <%= job.companytwitter %></a></p>
<p>Contract Type: <%= job.contract %></p>
<p>City: <%= job.city %></p>
<p>Expiry date: <%= job.expirydate %></p>
<p>Job Type: <%= job.jobtype %></p>
<p>Full Description:<br><br><%= job.description %></p>
<p>How to apply: <%= job.apply %></p>
</div>
<% end %>
job.rb -
class Job < ActiveRecord::Base
validates :position, presence: true
validates :company, presence: true
validates :salary, presence: true
validates :companywebsite, presence: true
validates :companytwitter, presence: true
validates :contract, presence: true
validates :city, presence: true
validates :expirydate, presence: true
validates :jobtype, presence: true
validates :description, presence: true
validates :apply, presence: true
end
jobs_controller.erb -
class JobsController < ApplicationController
def index
@jobs = Job.page(params[:page]).per(25)
end
def new
@job = Job.new
end
def create
@job = Job.new(params.require(:job).permit(:position, :company, :salary, :companywebsite, :companytwitter, :contract, :city, :expirydate, :jobtype, :description, :apply ))
if @job.save
redirect_to root_path
else
render "new"
end
end
end
new.html.erb -
<%= simple_form_for @job, html: { multipart: true } do |form| %>
<%= form.input :position, input_html: { maxlength: 60 }, placeholder: "Job Position", label: false %>
<%= form.input :company, input_html: { maxlength: 60 }, placeholder: "Company name", label: false %>
<%= form.input :salary, input_html: { maxlength: 60 }, placeholder: "Salary", label: false %>
<%= form.input :companywebsite, input_html: { maxlength: 60 }, placeholder: "Company Website", label: false %>
<%= form.input :companytwitter, input_html: { maxlength: 60 }, placeholder: "Twitter Handle e.g @Hatch_Inc", label: false %>
<%= form.input :contract, input_html: { maxlength: 60 }, placeholder: "Contract Type", label: false %>
<%= form.input :city, input_html: { maxlength: 60 }, placeholder: "City", label: false %>
<%= form.input :expirydate, input_html: { maxlength: 60 }, placeholder: "Expiry date", label: false %>
<%= form.input :jobtype, input_html: { maxlength: 60 }, placeholder: "Job Type", label: false %>
<%= form.input :description, input_html: { maxlength: 60 }, placeholder: "Full job description", label: false %>
<%= form.input :apply, input_html: { maxlength: 60 }, placeholder: "How to apply", label: false %>
<%= form.button :submit %>
<% end %>
Here's an example that uses JQuery to submit an AJAX request (so your page doesn't refresh every time a box is checked). In your view, you create a checkbox for each unique country. Jquery parameterizes the selected countries and submits them to your controller (specifying that you want to respond with JavaScript). A scope in your Jobs model applies the filter.
index.html.erb
<div id='job-list'>
<% @jobs.each do |job| %>
<div class="job">
<!-- Display your job here -->
</div>
<% end %>
</div>
<div id='countries'>
<h4> Country Filter: </h4>
<% @countries= Job.uniq.pluck(:country) %>
<% @countries.each do |c| %>
<br><input id="<%= c %>" type="checkbox" class="country-select" checked><label for="<%= c %>"> <%= c %> </label>
<% end %>
</div>
index.js.erb
var jobs = $('#job-list');
jobs.empty();
<% @jobs.each do |job|%>
jobs.append("<div class='job'><%= job %></div>"); // job display goes here
<% end %>
courses.coffee
getParams = ->
params = ""
countries = []
$(".country-select:checked").each ->
countries.push($(this).attr('id'))
params += "&#{$.param({countries: countries})}";
return params
$('.country-select').on 'change', (event) =>
$.ajax "/jobs.js?"+getParams(),
type: 'GET'
dataType: 'script'
Jobs controller
class JobsController < ApplicationController
respond_to :html, :js
def index
@jobs = Job.page(params[:page]).per(25).by_country(params[:countries])
end
end
Job model
class Job < ActiveRecord::Base
scope :by_country, -> (countries) { where(:country => (countries|| Course.uniq.pluck(:country)) ) }
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.