Search code examples
ruby-on-railsrubychartkick

'Kickchart gem' show monthly chart by selecting the momth and set by default current month to show


I have this code in my controller filter by month :

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

now I need to set current month as default value in case of nil ????


Solution

  • I guess you already implemented that method at your Model.

    You can get the name of the current month with:

    Date.today.strftime("%B")

    So at your code:

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

    This should be work if your method by_month search records by month name

    If your method search by month number then change Date.today.strftime("%B") by Date.today.strftime("%m")