Sunday, September 5, 2010

[Rails] Re: Whats a good way to avoid nil lookup errors

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...@gmail.com> 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...@gmail.com>
> 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@googlegroups.com.
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.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate