Couldn't find item with ID = 100


#1

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


#2

On Mar 29, 1:59 pm, derm_w removed_email_address@domain.invalid wrote:

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


#3

Can anyone please tell me how to fix this problem ?

You can use find_by_id, it returns nil if no record was found. It
depends on what you want to achieve.


#4

On Mar 29, 1:59 pm, derm_w removed_email_address@domain.invalid wrote:

Hi all.

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

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.


#5

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


#6

Derm -
On 30-Mar-09, at 12:11 PM, derm_w wrote:

Many thanks Wouter for your reply.

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.

Model.find(id) intentionally returns an exception when the row isn’t
found.

you can either trap the exception (rescue), or do a
Model.find_by_id(id) which won’t fire the exception.

Jodi


#7

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!


#8

On Mar 29, 1:59 pm, derm_w removed_email_address@domain.invalid wrote:

Hi all.

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

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.


#9

Thank you Jodi and Fernando.

Kind regards,

Dermot.