Alex Rivera | Logout

How to check if params[:some][:field] is nil?

Asked 2012-08-07T12:40:13.783
10

I tried code, that plused a lot of people - How to test if parameters exist in rails, but it didn't work():

     if ( params.has_key?([:start_date]) && params.has_key?([:end_date]) )

I think, that is because of complicated params and if I write this:

       if ( params.has_key?([:report][:start_date]) && params.has_key?([:report][:end_date]) )

gives me error

        can't convert Symbol into Integer

this doesn't work too:

           if ( params[:report][:start_date] && params[:report][:end_date] )

gives me error:

        undefined method `[]' for nil:NilClass

It always go into else statement.

Here are my params:

    report: 
    start_date: 01/08/2012
    end_date: 10/08/2012

Can someone help me ?

Edit
Report

1 Answer

6

It would seem you need the following

    params[:report].present?

or

    params[:report].nil?

or

    params[:report].empty?

depending on what you are trying to check for

answered 2012-08-07T12:47:23.500

Your Answer