# Verify presence\_of parameter

**URL:** <https://www.ruby-forum.com/t/verify-presence-of-parameter/183464>\
**Category:** Rails\
**Created:** [February 15, 2010, 4:07am UTC](https://www.ruby-forum.com/t/verify-presence-of-parameter/183464 "2010-02-15T04:07:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [February 15, 2010, 4:07am UTC](https://www.ruby-forum.com/t/verify-presence-of-parameter/183464/1 "2010-02-15T04:07:49Z")

</div>

I would like to verify a parameter is present before a search is posted.

min\_age = params[:min\_age]  
max\_age = params[:max\_age]

@users = User.find(:all,  
:conditions =\> ["years \>= ? AND years \<= ? " min\_age,  
max\_age])

Basically I am just concerned about the max\_age.

I tried this:  
min\_age = params[:min\_age]  
max\_age = params[:max\_age]

if max\_age.nil?  
flash[:notice] = “please enter max age!”  
redirect\_to :action =\> “index”

@users = User.find(:all,  
:conditions =\> ["years \>= ? AND years \<= ? " min\_age,  
max\_age])

But the search still ran with a nil max\_age parameter.

---

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [February 15, 2010, 9:35am UTC](https://www.ruby-forum.com/t/verify-presence-of-parameter/183464/2 "2010-02-15T09:35:30Z")

</div>

On 15 February 2010 03:07, Sean S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I tried this:  
> min\_age = params[:min\_age]  
> max\_age = params[:max\_age]
> 
> if max\_age.nil?  
> flash[:notice] = “please enter max age!”  
> redirect\_to :action =\> “index”

You need an else in here

Colin

---

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [February 15, 2010, 10:02am UTC](https://www.ruby-forum.com/t/verify-presence-of-parameter/183464/3 "2010-02-15T10:02:26Z")

</div>

On 15 February 2010 03:07, Sean S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I tried this:  
> min\_age = params[:min\_age]  
> max\_age = params[:max\_age]
> 
> if max\_age.nil?  
> flash[:notice] = “please enter max age!”

If there is a field called “max\_age” on the form, but the user leaves  
it blank, you won’t get nil from params, you’ll get an empty string.

So if you check for max\_age.blank? instead, you should get the result  
you want.

(Running a debugger would help you find this out very easily, or even  
just outputting the value of max\_age to the console to see if it’s  
what you expect it to be…)
