couldn't find item with ID = 100

Hi all.

I'm new to rails and am learning my way by putting together a simple
app.

I'm pulling items out of the params by using find(params['id']) and
showing results in a show view.

Things are going along nicely until I enter an ID into the address bar
that does not exist. Then the whole thing blows up and I get ACTIVE
RECORD::RECORD NOT FOUND IN PUBLIC CONTROLLER#SHOW along with
"couldn't find item with ID = 100" .

This is my show view code:-

                Id: <%= @album.id %><br/>
    Title: <%= @album.title %><br/>
    Artist: <%= @album.artist%><br/>
    Genre: <%= @album.genre %><br/>
    <% end -%><br/>

Can anyone please tell me how to fix this problem ?

Would really appreciate any help.

Many thanks,

derm_w

Hi all.

I'm new to rails and am learning my way by putting together a simple
app.

I'm pulling items out of the params by using find(params['id']) and
showing results in a show view.

Things are going along nicely until I enter an ID into the address bar
that does not exist. Then the whole thing blows up and I get ACTIVE
RECORD::RECORD NOT FOUND IN PUBLIC CONTROLLER#SHOW along with
"couldn't find item with ID = 100" .

That's what find does. It's up to you to rescue
ActiveRecord::RecordNotFound and do something different

Fred

Thanks for your replies, Frederick and Fernando.

I'm anticipating users playing around with url and putting in their
own random ID's.

So I would like to have a 'No record found' displayed when non
existant ID is inserted just like find_by_id does when it returns nil.

derm_w wrote:

So I would like to have a 'No record found' displayed when non
existant ID is inserted just like find_by_id does when it returns nil.

This would be something like:

def show
  @album = Album.find_by_id(params[:id])
  unless @album then
    # Here you could render a template or redirect to a different action
    render :template => 'record_not_found'
  end
  # When the album is found, the default template for this method is
rendered
end

Many thanks Wouter for your reply.

I've now got this:-

       def show
    @album = Album.find(params[:id])
    unless @album then
      render(:action => 'record_not_found')
    end
    render(:action => 'show_album')
  end

However I'm still getting "couldn't find album with ID=100" (or any
other number that doesn't exist)

I wonder what's amiss?

Dermot.

Derm -

I wonder what's amiss?

Dermot.

You don't read our posts... Use find_by_id or begin...rescue to trap the
error. Enough now!

Thank you Jodi and Fernando.

Kind regards,

Dermot.