When navigating to: http://localhost:3000/users/12 Whats a good way of avoiding the error: "Couldn''t find User with ID=12", which happens when i try to display a page of user_id 12 that doesnt exist in the database. Instead of showing the error. id like to show a page that says: "This user does not exist" Someone suggested using rescue_from. Is this the best solution for this? Or are there better alternative approaches? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
def show @user = User.find(params[:id]) rescue ActiveRecord::RecordNotFound redirect_to users_path, :alert => "This user does not exists" end -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
One of the solutions would be to use redirect_to and flash[:notice] like: @user = User.find(1) unless @user flash[:notice] = "can''t find user with given id" redirect_to users_path end I won''t say it''s the best but it''s still a solution :) On 5 sept., 13:55, Christian Fazzini <christian.fazz...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> When navigating to:http://localhost:3000/users/12 > > Whats a good way of avoiding the error: "Couldn''t find User with > ID=12", which happens when i try to display a page of user_id 12 that > doesnt exist in the database. > > Instead of showing the error. id like to show a page that says: "This > user does not exist" > > Someone suggested using rescue_from. Is this the best solution for > this? Or are there better alternative approaches?-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Yes but, it will never reach "unless." The system fails after @user User.find(1) since Active Record is trying to find a record that does not exists On Sep 5, 7:28 pm, Ugis Ozols <ugis.ozo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> One of the solutions would be to use redirect_to and flash[:notice] > like: > > @user = User.find(1) > unless @user > flash[:notice] = "can''t find user with given id" > redirect_to users_path > end > > I won''t say it''s the best but it''s still a solution :) > > On 5 sept., 13:55, Christian Fazzini <christian.fazz...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > When navigating to:http://localhost:3000/users/12 > > > Whats a good way of avoiding the error: "Couldn''t find User with > > ID=12", which happens when i try to display a page of user_id 12 that > > doesnt exist in the database. > > > Instead of showing the error. id like to show a page that says: "This > > user does not exist" > > > Someone suggested using rescue_from. Is this the best solution for > > this? Or are there better alternative approaches?-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
@user = User.find(1) if User.exists?(1) I don''t like doing two lookups for one record, but it works -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
What about @user = User.find(:first, :conditions => {:id => 1}) if (@user) blah, blah, blah end It''s longer to type, but returns nil if the user doesn''t exist. On Sun, 05 Sep 2010 21:21:25 +0800, Michael Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> @user = User.find(1) if User.exists?(1) > > I don''t like doing two lookups for one record, but it works-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sep 5, 2010, at 9:30 AM, Simon Macneall wrote:> On Sun, 05 Sep 2010 21:21:25 +0800, Michael Pavling > <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> @user = User.find(1) if User.exists?(1) >> >> I don''t like doing two lookups for one record, but it works > > What about > @user = User.find(:first, :conditions => {:id => 1}) > if (@user) > blah, blah, blah > end > > It''s longer to type, but returns nil if the user doesn''t exist.Ugis almost has it. Try this @user = User.find_by_id(1) unless @user flash[:notice] = "can''t find user with given id" redirect_to users_path end ActiveRecord::Base#find_by_id is a dynamic finder that will work just like the line Simon has above--nil if there is no record found. -Rob Rob Biedenharn Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org http://AgileConsultingLLC.com/ rab-/VpnD74mH8+00s0LW7PaslaTQe2KTcn/@public.gmane.org http://GaslightSoftware.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
So this would be the convention right? not by using rescue_from. Is this right? On Sep 5, 9:42 pm, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote:> On Sep 5, 2010, at 9:30 AM, Simon Macneall wrote: > > > On Sun, 05 Sep 2010 21:21:25 +0800, Michael Pavling > > <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >> @user = User.find(1) if User.exists?(1) > > >> I don''t like doing two lookups for one record, but it works > > > What about > > @user = User.find(:first, :conditions => {:id => 1}) > > if (@user) > > blah, blah, blah > > end > > > It''s longer to type, but returns nil if the user doesn''t exist. > > Ugis almost has it. Try this > > @user = User.find_by_id(1) > unless @user > flash[:notice] = "can''t find user with given id" > redirect_to users_path > end > > ActiveRecord::Base#find_by_id is a dynamic finder that will work just > like the line Simon has above--nil if there is no record found. > > -Rob > > Rob Biedenharn > R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org http://AgileConsultingLLC.com/ > r...-/VpnD74mH8+00s0LW7PaslaTQe2KTcn/@public.gmane.org http://GaslightSoftware.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Hi Christian On Sun, Sep 5, 2010 at 9:38 AM, Christian Fazzini <christian.fazzini-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> So this would be the convention right? not by using rescue_from. Is > this right?Yes. When possible, avoid begin-rescue by using Ruby / Rails to return a value you can handle within the normal course of your application logic. It makes for more readable code. Best regards, Bill -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi Paul. but wouldnt that redirect to users_path on any activerecord not found event? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Good call on that find_by_id. Noted to myself :) On 5 sept., 16:42, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote:> On Sep 5, 2010, at 9:30 AM, Simon Macneall wrote: > > > On Sun, 05 Sep 2010 21:21:25 +0800, Michael Pavling > > <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >> @user = User.find(1) if User.exists?(1) > > >> I don''t like doing two lookups for one record, but it works > > > What about > > @user = User.find(:first, :conditions => {:id => 1}) > > if (@user) > > blah, blah, blah > > end > > > It''s longer to type, but returns nil if the user doesn''t exist. > > Ugis almost has it. Try this > > @user = User.find_by_id(1) > unless @user > flash[:notice] = "can''t find user with given id" > redirect_to users_path > end > > ActiveRecord::Base#find_by_id is a dynamic finder that will work just > like the line Simon has above--nil if there is no record found. > > -Rob > > Rob Biedenharn > R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org http://AgileConsultingLLC.com/ > r...-/VpnD74mH8+00s0LW7PaslaTQe2KTcn/@public.gmane.org http://GaslightSoftware.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
nope, only within the action. On Sep 5, 11:31 pm, Christian Fazzini <christian.fazz...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi Paul. but wouldnt that redirect to users_path on any activerecord > not found event?-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.