# Testing for non-existent parameters with "if"

**URL:** <https://www.ruby-forum.com/t/testing-for-non-existent-parameters-with-if/87601>\
**Category:** Rails\
**Created:** [March 4, 2007, 8:09am UTC](https://www.ruby-forum.com/t/testing-for-non-existent-parameters-with-if/87601 "2007-03-04T08:09:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [March 4, 2007, 8:09am UTC](https://www.ruby-forum.com/t/testing-for-non-existent-parameters-with-if/87601/1 "2007-03-04T08:09:01Z")

</div>

Rails gives me an error when I test for a blank param like this:

```
    if params[:post][:id]
      @comment.commentable = Post.find(params[:post][:id])
    elsif params[:event][:id]
      @comment.commentable = Event.find(params[:event][:id])
    end

```

If I have params[:post][:id], things are fine. But if I have  
params[:event][:id] instead, I get this error:

You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occurred while evaluating nil.[]

Rails dies when it checks for the non-existent params[:post][:id]. I  
will always send either :event or :post, but never both. How can I use  
“if - elsif” to test for this without crashing? Thanks!

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [March 4, 2007, 8:31am UTC](https://www.ruby-forum.com/t/testing-for-non-existent-parameters-with-if/87601/2 "2007-03-04T08:31:35Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Hi Taylor,

On 3-Mar-07, at 11:09 PM, Taylor S. wrote:

> You have a nil object when you didn’t expect it!  
> You might have expected an instance of Array.  
> The error occurred while evaluating nil.[]
> 
> Rails dies when it checks for the non-existent params[:post][:id]. I  
> will always send either :event or :post, but never both. How can I  
> use  
> “if - elsif” to test for this without crashing? Thanks!

If the params[:post] hash is non-existent, then checking within it  
to see if there’s an :id value is probably what’s throwing that error.

To avoid this problem you’d want to do something like:

if params[:post] && params[:post][:id]  
@comment.commentable = Post.find(params[:post][:id])  
elsif params[:event] && params[:event][:id]  
@comment.commentable = Event.find(params[:event][:id])  
end

Or you could do something like this:

if id = params[:post][:id] rescue nil  
@comment.commentable = Post.find(id)  
elsif id = params[:event][:id] rescue nil  
@comment.commentable = Event.find(id)  
end

* * *

Thanks,

Dan

* * *

Dan K.  
Autopilot Marketing Inc.

Email: removed\_email\_address@domain.invalid  
Phone: 1 (604) 820-0212  
Web: [http://autopilotmarketing.com/](http://autopilotmarketing.com/)  
vCard: [http://autopilotmarketing.com/~dan.kubb/vcard](http://autopilotmarketing.com/~dan.kubb/vcard)

* * *

-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.2.2 (Darwin)

iD8DBQFF6nWO4DfZD7OEWk0RAhpXAJsH0ZYbp1Rt5aNZEIUtTa7Z8q9d3ACdHwdm  
X6whYvFwH02fEBYi1zMF1v0=  
=KFsJ  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [March 4, 2007, 9:24am UTC](https://www.ruby-forum.com/t/testing-for-non-existent-parameters-with-if/87601/3 "2007-03-04T09:24:37Z")

</div>

Taylor S. wrote:

> You have a nil object when you didn’t expect it!  
> You might have expected an instance of Array.  
> The error occurred while evaluating nil.[]
> 
> Rails dies when it checks for the non-existent params[:post][:id]. I  
> will always send either :event or :post, but never both. How can I use  
> “if - elsif” to test for this without crashing? Thanks!

It depends on what is nil, i.e. params[:post] or params[:post][:id] but  
you can do something like:

if params[:post].has\_key?(:id)

or

if not params[:post][:id] == nil

–  
Michael W.

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [March 4, 2007, 11:54am UTC](https://www.ruby-forum.com/t/testing-for-non-existent-parameters-with-if/87601/4 "2007-03-04T11:54:45Z")

</div>

> Rails gives me an error when I test for a blank param like this:
> 
> ```
> if params[:post][:id]
> 
> ```
> 
> If I have params[:post][:id], things are fine. But if I have  
> params[:event][:id] instead, I get this error:  
> And a third solution yet  
> if defined? params[:post][:id]  
> else  
> end

It will not always work if you use it from a view (because of how local  
variables are handled), but for Models, Controllers and the sort it  
should be fine.

regards,  
javier ramirez

* * *

Estamos de estreno… si necesitas llevar el control de tus gastos  
visita [http://www.gastosgem.com](http://www.gastosgem.com) !!Es gratis!!

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [March 4, 2007, 4:16pm UTC](https://www.ruby-forum.com/t/testing-for-non-existent-parameters-with-if/87601/5 "2007-03-04T16:16:16Z")

</div>

Thanks to all - these solutions worked. I ended up using “if  
params[:post] && params[:post][:id]”
