'Kickchart gem' 选择月份显示月度图表,默认显示当前月份
'Kickchart gem' show monthly chart by selecting the momth and set by default current month to show
我的控制器过滤器中有这段代码按月计算:
@posts = Post.by_month(params[:selected_month])
现在我需要将当前月份设置为默认值以防 nil ????
我猜你已经在你的模型中实现了那个方法。
您可以通过以下方式获取当前月份的名称:
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")
我的控制器过滤器中有这段代码按月计算:
@posts = Post.by_month(params[:selected_month])
现在我需要将当前月份设置为默认值以防 nil ????
我猜你已经在你的模型中实现了那个方法。
您可以通过以下方式获取当前月份的名称:
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")