# An optional params

**URL:** <https://www.ruby-forum.com/t/an-optional-params/119312>\
**Category:** Rails\
**Created:** [November 5, 2007, 11:39pm UTC](https://www.ruby-forum.com/t/an-optional-params/119312 "2007-11-05T23:39:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a3d4f5/32.png) [@chil](https://www.ruby-forum.com/u/chil)\
**Post date:** [November 5, 2007, 11:39pm UTC](https://www.ruby-forum.com/t/an-optional-params/119312/1 "2007-11-05T23:39:22Z")

</div>

Hi!

In order to respect DRY principles, I had to regroup two controller in  
one. To sum up, I have to build one controller which can make a ‘simple’  
page and this same page with other features when a second id param is  
entered in the url.

This is why, I modified the file ‘routes.rb’ so the action could access  
to 2 params: :id and :tuto\_id

In the controller, only :id is mandatory  
I would like to control if the url contains the param :tuto\_id

I wrote the controller:  
unless params[:tuto\_id].nil?  
if Tuto.exists? params[:tuto\_id]  
@tuto = Tuto.find(params[:tuto\_id])…

And in the view when I want to show more objects when the url contains  
params[:tuto\_]id  
\<% unless @tuto.nil? %\>…

When the url contains just the params :id, there is no problems but when  
it contains the two params, I have the error:  
Called id for nil, which would mistakenly be 4 – if you really wanted  
the id of nil, use object\_id

Is my strategy the right one ?  
Does this error refers to a another problem ?

Thanxs

---

<div class="post-metadata">

**Author:** ![chil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a3d4f5/32.png) [@chil](https://www.ruby-forum.com/u/chil)\
**Post date:** [November 6, 2007, 5:47pm UTC](https://www.ruby-forum.com/t/an-optional-params/119312/2 "2007-11-06T17:47:39Z")

</div>

On 11/5/07, Achille P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> In the controller, only :id is mandatory  
> I would like to control if the url contains the param :tuto\_id
> 
> I wrote the controller:  
> unless params[:tuto\_id].nil?  
> if Tuto.exists? params[:tuto\_id]  
> @tuto = Tuto.find(params[:tuto\_id])…

This could be simplified:

@tuto = Tuto.find(params[:tuto\_id]) if params[:tuto\_id]

(This will raise an exception if an invalid tuto\_id is passed)

> And in the view when I want to show more objects when the url contains  
> params[:tuto\_]id  
> \<% unless @tuto.nil? %\>…

The nil? test is unecessary. You can just use \<% if @tuto %\>. But what  
you have should work fine.

> When the url contains just the params :id, there is no problems but when  
> it contains the two params, I have the error:  
> Called id for nil, which would mistakenly be 4 – if you really wanted  
> the id of nil, use object\_id
> 
> Is my strategy the right one ?  
> Does this error refers to a another problem ?

The error is coming from somewhere else. Look at the backtrace  
information in the error report. It looks like you’re probably using  
@tuto.id somewhere, but @tuto is nil.

---

<div class="post-metadata">

**Author:** ![chil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a3d4f5/32.png) [@chil](https://www.ruby-forum.com/u/chil)\
**Post date:** [November 6, 2007, 6:40pm UTC](https://www.ruby-forum.com/t/an-optional-params/119312/3 "2007-11-06T18:40:17Z")

</div>

Thanxs, I found the error, you were right: it was somewhere else  
Thanxs for the simplifications too !
