使用最后一个和下一个按钮按月/年遍历记录 Rails 6



我正在创建一个双按钮功能,它允许我通过从当前月份减去参数或移动到下个月来将参数传递给按钮。理论上很简单,您单击下一步,您将看到为该月创建的所有记录,或者单击上个月,您将看到上个月创建的记录。我遇到的问题是,当我单击下一步按钮时,参数将是一次性的。如何使用两个按钮浏览月/年?它似乎没有记住最后一个按钮单击事件的参数。

轨道日志

Started GET "/facility_directory/lnpp?previous_month=5&commit=Next&next_month=7" for 127.0.0.1

facilities_controller.rb

def facility_directory
if params.has_key?(:select) # select month/year form
month = Date.new(params[:select][:year].to_i, params[:select][:month].to_i)
@facility_records = @facility.facility_records.where(created_at: month.all_month).order(created_at: :desc)
@year = params[:select][:year]
@month = params[:select][:month]
elsif params.has_key?(:previous_month) # last month navigation button
previous_month = Date.new(Date.today.year, params[:previous_month].to_i)
@facility_records = @facility.facility_records.where(created_at: previous_month.all_month).order(created_at: :desc)
elsif params.has_key?(:next_month)
next_month = Date.new(Date.today.year, params[:next_month].to_i)
@facility_records = @facility.facility_records.where(created_at: next_month.all_month).order(created_at: :asc)
else
date = Date.today
start_date = date.at_beginning_of_month
end_date = date.at_end_of_month
@facility_records = @facility.chart_records.order(created_at: :desc).where(created_at: start_date..end_date)
end

结束

facility_directory.html.erb

<%= form_tag facility_directory_path(@facility), :method => :get, :class => "form" do %>
<div>
<%= select_month(Date.today, {:include_blank => true.....%>
<%= select_year(Date.today, {:include_blank => true...%>
</div>
<%= submit_tag('Search') %>
<% end %>

上个月和下个月的导航按钮。

<%= form_tag facility_directory_path(@facility), method: :get, class: '' do %>
<%= submit_tag 'Previous Month', class: 'button' %>
<%= hidden_field_tag 'previous_month', @month.present? ? (@month.to_i - 1) : (Time.now.month - 1) %>
<%= submit_tag 'Next', class: 'button ui basic blue small' %>
<%= hidden_field_tag 'next_month', @month.present? ? (@month.to_i + 1) : (Time.now.month + 1) %>
<% end %>

看起来您也会遇到一个问题,即您的视图将呈现一个表单,其中next_month的隐藏值将为 13,或者前一个将为 0 - 两者都无效。

您还将在视图中设置默认的年份和月份逻辑。如果月份和年份的设置在一个地方完成 - 控制器,您通常会得到更好的服务。

将所有逻辑(包括如何计算下一个/上一个是什么(转移到控制器中,并保持视图简单:

<%= form_tag facility_directory_path(@facility), method: :get, class: '' do %>
<%= hidden_field_tag 'current_month', @month %>
<%= hidden_field_tag 'current_year', @year %>
<%= submit_tag 'Previous Month', class: 'button' %>
<%= submit_tag 'Next', class: 'button ui basic blue small' %>
<% end %>

视图中的此窗体现在仅保存当前状态(月份和年份(,并具有上一个和下一个。没有月份计算或默认设置。

控制器

然后,在控制器中,您可以检测是否:

  • 没有表单输入(默认使用当前月份(
  • 按下搜索按钮(使用年份和月份下拉菜单(
  • 按下下一个或上一个按钮。

按下的按钮的值将传递给控制器,因此如果按下下一个或上一个按钮,您将看到params[:commit]等于"上个月"或"下一个"。而且,你还会有参数[:current_year]和参数[:current_month]。

确保您的控制器创建默认@year并@month为当前。

下面应该涵盖所有这些情况。

if params[:commit] == "Previous Month" || params[:commit] == "Next"
# Handle preview/next buttons
now = Date.new(params[:current_year].to_i, params[:current_month].to_i, 1)
if params[:commit] == "Previous Month"
target_date = now - 1.month
else
target_date = now + 1.month
end
elsif params.has_key?(:select)
# Handle the select drop down
target_date = Date.new(params[:select][:year].to_i, params[:select][:month].to_i)
else
# Handle the default case - the first day of the current month
target_date = Date.new(Date.today.year, Date.today.month, 1)
end
@year = target_date.year.to_i
@month = target_date.month.to_i
# You can now consolidate the Active Record query into one query which handles all cases
@facility_records = @facility.chart_records.order(created_at: :desc).where(created_at: target_date..target_date+1.month)

使用此方法,您可以有一个适用于所有实例的活动记录查询。

将来避免这种情况的最好办法是:

  1. 将尽可能多的逻辑排除在视图之外
  2. 在控制器(或模型(中设置操作的默认值
  3. 通过考虑案例来设计控制器
  4. 知道您可以读取按下的按钮的值。

希望这对现在和将来都有帮助。

最新更新