我有一个工作列表项目设置,我希望能够通过几个过滤器进行过滤。我希望能够使用可以按某些元素过滤的侧边栏 - :city,:jobtype和:contracttype
是否有一种直接的方式来创建单选按钮,显示用户可用的选项,例如:城市伦敦,曼彻斯特,布莱顿等列表,可以勾选显示这些特定的工作?
我是铁杆新手,所以很难解决我需要做的事情,如果有人能解释我需要做什么,我真的很感激!
我的代码如下:
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 %>
答案 0 :(得分:1)
这是一个使用JQuery提交AJAX请求的示例(因此每次检查框时您的页面都不会刷新)。在您的视图中,您为每个唯一国家/地区创建一个复选框。 Jquery参数化所选国家/地区并将它们提交给您的控制器(指定您要使用JavaScript进行响应)。作业模型中的作用域应用过滤器。
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'
乔布斯控制器
class JobsController < ApplicationController
respond_to :html, :js
def index
@jobs = Job.page(params[:page]).per(25).by_country(params[:countries])
end
end
工作模式
class Job < ActiveRecord::Base
scope :by_country, -> (countries) { where(:country => (countries|| Course.uniq.pluck(:country)) ) }
end