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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 29, 1:59 pm, derm_w <dermotw...-zLKRkbt3P8esTnJN9+BGXg@public.gmane.org> wrote:> 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> 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 29, 1:59 pm, derm_w <dermotw...-zLKRkbt3P8esTnJN9+BGXg@public.gmane.org> 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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 29, 1:59 pm, derm_w <dermotw...-zLKRkbt3P8esTnJN9+BGXg@public.gmane.org> 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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Derm - On 30-Mar-09, at 12:11 PM, derm_w wrote:> > > > On Mar 29, 1:59 pm, derm_w <dermotw...-zLKRkbt3P8esTnJN9+BGXg@public.gmane.org> 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.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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> 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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thank you Jodi and Fernando. Kind regards, Dermot. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---