1

我尝试学习 RoR 并拥有以下代码。我只是认为它可以容易得多。我从 RoR 中了解到的是,你应该尽量不要重复,让事情尽可能简单。所以也许有人对此有一些建议:

    if params[:select_date].present?
  if params[:select_date] == '1'
    params[:search][:date_started_at_gte] = Date.today.beginning_of_month
  end
  if params[:select_date] == '2'
    params[:search][:date_started_at_gte] = Date.today - 1.months
  end
  if params[:select_date] == '3'
    params[:search][:date_started_at_gte] = Date.today - 3.months
  end
  if params[:select_date] == '0'
    params[:search][:date_started_at_gte] = params[:search][:date_started_at_gte]
    params[:search][:date_started_at_lte] = params[:search][:date_started_at_lte]
  end
else
    params[:search][:date_started_at_gte] = Date.today.beginning_of_month
    params[:search][:date_started_at_lte] = Date.today
end

谢谢!

4

1 回答 1

0
gte = lte = ''

case params[:select_date]
when '1'
  gte = Date.today.beginning_of_month
when '2'
  gte = Date.today - 1.months
#...
when '0'
  gte = params[:search][:date_started_at_gte]
  lte = params[:search][:date_started_at_lte]
else
  gte = Date.today.beginning_of_month
  lte = Date.today
end

我在这里使用了一些新变量gte, lte。我强烈建议您不要更改 params 的值!

于 2013-07-06T06:36:05.613 回答