an optional params


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]).......

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 somewhere, but @tuto is nil.