Forum: Ruby on Rails couldn't find item with ID = 100

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
derm_w (Guest)
on 2009-03-29 18:10
(Received via mailing list)
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
Frederick C. (Guest)
on 2009-03-29 18:12
(Received via mailing list)
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
Fernando P. (Guest)
on 2009-03-29 21:19
> 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.
derm_w (Guest)
on 2009-03-29 21:56
(Received via mailing list)
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.
Wouter d. (Guest)
on 2009-03-30 03:49
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
derm_w (Guest)
on 2009-03-30 20:11
(Received via mailing list)
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.
Jodi S. (Guest)
on 2009-03-30 20:14
(Received via mailing list)
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
Fernando P. (Guest)
on 2009-03-30 20:14
> 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!
derm_w (Guest)
on 2009-03-30 21:09
(Received via mailing list)
Thank you Jodi and Fernando.

Kind regards,

Dermot.
This topic is locked and can not be replied to.