【问题标题】:ransack gem: Filter for years and months in dropdownransack gem:在下拉列表中过滤年和月
【发布时间】:2018-01-17 09:14:12
【问题描述】:

我使用 Rails 5.1 和 ransack gem 的当前版本。

我有一个“统计”页面,我在其中显示工作,我想使用两个下拉选择字段实现过滤功能,1)年(2011-2018)和 2)月(1 月至 12 月)。我怎样才能做到这一点?

我在想类似的东西

# in statistics index view
<%= search_form_for @search do |f| %>
  <%= f.grouping_fields(f.object.new_grouping) do |g| %>
    <%= g.condition_fields(g.object.new_condition) do |c| %>
      <%= # here the years dropdown list%>

      <%= # here the months dropdown list%>
      <% end %>
    <% end %>
  <% end %>
  <%= f.submit "filter" %>
<% end %>

它应该是什么样子的?

【问题讨论】:

  • 那些年是否与您的模特有关?目前,我看不出有任何嵌套输入的理由,您只需创建两个独立的下拉列表(年、月)...
  • 是的,你是对的!我想的太复杂了……
  • @Trinity76 你的模型中有年份和月份列吗?

标签: ruby-on-rails ransack


【解决方案1】:

我不确定它是否可以通过洗劫来完成,您可以选择使用简单的形式来做到这一点:- 像这样:-

<%= form_tag jobs_path, method: :get%>
  <%= select_year(Date.today, {:prompt => "Year",:start_year => DateTime.now.year,:end_year => DateTime.now.year - 7, prefix: 'start_date'},{:field_name => 'year', :id => 'start-year'}) %>
  <%= select_month(Date.today, {:prompt => "month",use_short_month: true, prefix: 'start_date'},{:field_name => 'month', :id => 'start-month'}) %>
  <%= submit_tag("Search Job", :id=>"button", :class=>"Test", :name=>"submit") %>
<%end%>

在您的索引操作中

def index
  if params[:year].present? or params[:month].present?
   #@jobs = Job.where("colum BETWEEN ? AND ?", ...)
   #@jobs = your_own_query_for_filter 
  else
    @jobs = Job.all
  end
end

我没有正确编写代码,这只是一个示例,您也可以这样做。希望对你有帮助。

在这里找到for date select form helper

【讨论】:

  • 我在视图中使用了您的代码 sn-p 并将控制器修改为:def index if params[:year].present?或 params[:month].present? @jobs = Job.where("datum_auftrag BETWEEN #{params[:year]}-#{params[:month]}-01 AND #{Time.now.strftime("%Y-%m-%d")} ") else #here the else branch end end 但是点击提交不会影响任何事情。你知道我错过了什么吗?
  • 我也试过这个:if params[:year].present?或 params[:month].present? @jobs = Job.where("datum_auftrag BETWEEN ?-?-01 AND #{Time.now.strftime("%Y-%m-%d")},params[:year],params[:month]")
  • 我创建了一个新帖子:stackoverflow.com/questions/48301147/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多