【问题标题】:'Kickchart gem' show monthly chart by selecting the momth and set by default current month to show'Kickchart gem' 通过选择月份来显示月度图表并默认设置当前月份来显示
【发布时间】:2017-09-09 18:36:45
【问题描述】:

我的控制器中有这个代码按月过滤:

@posts = Post.by_month(params[:selected_month])                           

现在我需要将当前月份设置为默认值以防 nil ????

【问题讨论】:

标签: ruby-on-rails ruby chartkick


【解决方案1】:

我猜你已经在你的模型中实现了那个方法。

您可以通过以下方式获取当前月份的名称:

Date.today.strftime("%B")

所以在你的代码中:

month = params[:selected_month] ? params[:selected_month] : Date.today.strftime("%B")
@posts = Post.by_month(month)

如果您的方法 by_month 按月份名称搜索记录,这应该可以工作

如果您的方法按月数搜索,则将 Date.today.strftime("%B") 更改为 Date.today.strftime("%m")

【讨论】:

  • 谢谢 Oscar Luza 我使用了你们中的一些代码并创建了 if 语句以防万一 nil 并且效果很好:if (params[:selected_month]).nil? #posts = Post.by_month(Date.today.strftime("%m")) else #posts = Post.by_month(params[:desierd_month]) end
  • 不客气!感谢您选择我的正确答案
猜你喜欢
  • 2017-05-23
  • 2018-03-02
  • 2018-10-24
  • 2018-10-03
  • 1970-01-01
  • 2011-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多