# Empty parameter submitted but .nil failed to detect it

**URL:** <https://www.ruby-forum.com/t/empty-parameter-submitted-but-nil-failed-to-detect-it/177216>\
**Category:** Rails\
**Created:** [October 13, 2009, 1:38pm UTC](https://www.ruby-forum.com/t/empty-parameter-submitted-but-nil-failed-to-detect-it/177216 "2009-10-13T13:38:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ct9a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@ct9a](https://www.ruby-forum.com/u/ct9a)\
**Post date:** [October 13, 2009, 1:38pm UTC](https://www.ruby-forum.com/t/empty-parameter-submitted-but-nil-failed-to-detect-it/177216/1 "2009-10-13T13:38:46Z")

</div>

hi guys

i have a form which has an element, “category id”.

When i submit it with values, i can see param(:category) with a value  
(as i print out that value in the controller’s method.

Anyway, when i submit my form without selecting any category, the  
method in the controller does not pick up on it.  
I tried using .nil? but it doesn’t work.

This is my code:  
def get\_subcategories  
if params[:category]  
puts " I M in GET SUBCATEGORIES -" + params[:category] +  
‘…’  
@category = Category.find(params[:category])  
@sub\_categories = @category.sub\_categories.all  
else  
puts “no given category id to retrieve the subcategory”  
end  
end

When i run the application and submit without a category value , i  
notice that params[:category]  
is still defined (hence printing out “I M in GET SUBCATEGORIES”) when  
it should not be.  
I have even tried “params[:category].nil?”

Any ideas? In perl, i could test the variable for truth (IE if  
($params-\>{‘category’}) then …

---

<div class="post-metadata">

**Author:** ![ct9a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@ct9a](https://www.ruby-forum.com/u/ct9a)\
**Post date:** [October 13, 2009, 2:13pm UTC](https://www.ruby-forum.com/t/empty-parameter-submitted-but-nil-failed-to-detect-it/177216/2 "2009-10-13T14:13:18Z")

</div>

2009/10/13 ct9a [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Â I tried using .nil? but it doesn’t work.  
> Â Â Â Â end  
> Â Â end
> 
> When i run the application and submit without a category value , i  
> notice that params[:category]  
> is still defined (hence printing out “I M in GET SUBCATEGORIES”) when  
> it should not be.  
> I have even tried “params[:category].nil?”

Would .blank? do what you want?

Colin

---

<div class="post-metadata">

**Author:** ![ct9a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@ct9a](https://www.ruby-forum.com/u/ct9a)\
**Post date:** [October 13, 2009, 2:15pm UTC](https://www.ruby-forum.com/t/empty-parameter-submitted-but-nil-failed-to-detect-it/177216/3 "2009-10-13T14:15:04Z")

</div>

On Tue, Oct 13, 2009 at 8:11 AM, Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > Anyway, when i submit my form without selecting any category, the  
> > else
> 
> Would .blank? do what you want?
> 
> Colin
> 
> Colin’s right - I would code: if not params[:category].blank?

–  
[www.abetaday.com](http://www.abetaday.com)

---

<div class="post-metadata">

**Author:** ![ct9a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@ct9a](https://www.ruby-forum.com/u/ct9a)\
**Post date:** [October 13, 2009, 3:08pm UTC](https://www.ruby-forum.com/t/empty-parameter-submitted-but-nil-failed-to-detect-it/177216/4 "2009-10-13T15:08:55Z")

</div>

Hi friend,

U can try this,

if params[:catergory] != nil
