-1

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 ????

neo7
  • 89
  • 1
  • 10
  • There are several answers on StackOverflow to help with default parameters for Ruby: https://stackoverflow.com/search?q=ruby+default+parameter – stealththeninja Sep 16 '17 at 04:02

1 Answers1

1

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")

Oscar Luza
  • 384
  • 3
  • 14
  • Thank you Oscar Luza I used some of you code and created if statement in case nil and worked great: if (params[:selected_month]).nil? #posts = Post.by_month(Date.today.strftime("%m")) else #posts = Post.by_month(params[:desierd_month]) end – neo7 Sep 09 '17 at 20:55
  • You're welcome!, Thank you for selecting my answer as correct – Oscar Luza Sep 09 '17 at 20:57