Rails:从下拉列表中搜索年份和月份 [英] Rails: search for years and months from drop down lists
问题描述
我使用 Rails 5.1,我正在尝试使用两个下拉选择列表 -1 进行搜索.年,2. 个月 - 和一个提交按钮.
I use Rails 5.1 and I'm trying to search with two dropdown select lists -1. years, 2. months - and a submit button.
在我看来:
<%= form_tag jobs_path, method: :get do %>
<%= select_year(Date.today, {:prompt => "Year",:start_year => DateTime.now.year,:end_year => DateTime.now.year - 7, prefix: 'select'},{:field_name => 'year', :id => 'start-year'}) %>
<%= select_month(Date.today, {:prompt => "month",use_short_month: true, prefix: 'select'},{:field_name => 'month', :id => 'start-month'}) %>
<%= submit_tag("Search Job", :id=>"button", :class=>"Test", :name=>"submit") %>
<%end%>
在我的 jobs_controller.rb 中:
In my jobs_controller.rb:
# GET /jobs.json
def index
if params[:year].present? or params[:month].present?
month_with_leading_zero = params[:month].to_s.rjust(2, "0")
start_date = "#{params[:year]}-#{month_with_leading_zero}-1"
end_date = Time.now.strftime("%Y-%m-%d")
@jobs = Job.where("datum_auftrag BETWEEN ? AND ?", start_date, end_date)
else
@search = Job.ransack(params[:q])
@jobs = @search.result
end
end
在我的数据库中,日期以格式保存:yyyy-mm-dd 例如.2011-01-20
In my Database the date is saved in format: yyyy-mm-dd eg. 2011-01-20
点击提交按钮会忽略过滤,而是打印所有作业:
A click on the submit button ignores the filtering instead prints all jobs:
开始获取"/jobs?utf8=%E2%9C%93&start_date%5Byear%5D=2016&start_date%5Bmonth%5D=1&submit=Search+Job"for ::1 at 2018-01-17 14:01:54 +0100 处理者JobsController#index 作为 HTML 参数:{"utf8"=>"✓","start_date"=>{"year"=>"2016", "month"=>"1"}, "submit"=>"Search Job"}在 layouts/application 中渲染 jobs/index.html.erbjobs/_job_links.html.erb (38.5ms) 作业负载 (5.5ms) SELECT jobs
.*FROM jobs
(1.3ms) SELECT COUNT() FROM manuscripts
WHEREmanuscripts
.job_id
= 20 (2.2ms) SELECT COUNT() FROMmanuscripts
WHERE manuscripts
.job_id
= 21
Started GET "/jobs?utf8=%E2%9C%93&start_date%5Byear%5D=2016&start_date%5Bmonth%5D=1&submit=Search+Job" for ::1 at 2018-01-17 14:01:54 +0100 Processing by JobsController#index as HTML Parameters: {"utf8"=>"✓", "start_date"=>{"year"=>"2016", "month"=>"1"}, "submit"=>"Search Job"} Rendering jobs/index.html.erb within layouts/application Rendered jobs/_job_links.html.erb (38.5ms) Job Load (5.5ms) SELECT
jobs
.* FROMjobs
(1.3ms) SELECT COUNT() FROMmanuscripts
WHEREmanuscripts
.job_id
= 20 (2.2ms) SELECT COUNT() FROMmanuscripts
WHEREmanuscripts
.job_id
= 21
我错过了什么?
推荐答案
1 在您的索引页上制作一个搜索表单
1 make a search form on your index page
<%= form_tag your_index_action_path, method: :get do%>
<%= select_year(Date.today, {:prompt => "Year",:start_year => DateTime.now.year,:end_year => DateTime.now.year - 7, prefix: 'select'},{:field_name => 'year', :id => 'start-year'}) %>
<%= select_month(Date.today, {:prompt => "month",use_short_month: true, prefix: 'select'},{:field_name => 'month', :id => 'start-month'}) %>
<%= submit_tag("Search Job", :id=>"button", :class=>"Test", :name=>"submit") %>
<%end%>
注意:- 将 your_index_action_path
替换为索引操作的路径,您可以通过运行 rake routes
note:- replace your_index_action_path
by path of your index action you ca get your path on console by running rake routes
2- 在您的索引操作中单击控制器端的提交按钮,您将获得以下参数
2- on click submit button at controller side in you index action you will get following paramter
{"utf8"=>"✓", "select"=>{"year"=>"2018", "month"=>"1"}, "submit"=>"Search Job", "controller"=>"jobs", "action"=>"index"} permitted: false>
所以从这里你只需要 params[:select][:year]
和 params[:select][:month]
so from here you need only params[:select][:year]
and params[:select][:month]
def index
if params[:select][:year].present? || params[:select][:month].present?
start_date = (params[:select][:year]+"-" +params[:select][:month]+"-"+Date.today.day.to_s).to_datetime.beginning_of_day
end_date = Date.today.end_of_day
@jobs = Job.where("datum_auftrag BETWEEN ? AND ?",start_date, end_date)
else
@jobs = Job.all
end
end
注意:-这里需要根据选定的值制作自定义日期,我使用了beginning_of_day
和end_of_day
方法来获得更清晰的搜索结果.
Note: -here it need to make custom date according to selected value and i have usedbeginning_of_day
and end_of_day
method to get more clear search result.
之前我只给了你一个示例代码,你应该修改它,在这里我再次给你解决方案,请不要只是复制和粘贴使用调试器并在控制器端撬来选择日期和年份,但是我实际上已经完成了这个答案.
Earlier i have given you just a sample code, and you were expected to modify it, here i m giving you again solution , please do not just copy and paste use debugger and pry at controller side to get selected date and year, however i m giving this answer practically done.
这篇关于Rails:从下拉列表中搜索年份和月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!