ActiveRecord Not found redirect

2 views
Skip to first unread message

Katherine

unread,
Dec 2, 2009, 9:12:22 PM12/2/09
to Philippine Ruby Users Group (PRUG/PhRUG)
Hi is it advisable to redirect per controller if a record is not
found?
Does anyone know a cleaner and better solution to this.
I am redirecting for every show action if a record is not found.

The advantage is that I am able to say "that article does not exist."
and list articles below it. Or if it's for users, "Sorry, that user
does not exist.."

I also have a catch all route but of course, that does not solve
ActiveRecord not found issue.

Any thoughts on this could help. Thanks very much... Happy Holidays!

Richard Gonzales

unread,
Dec 2, 2009, 9:19:12 PM12/2/09
to ruby...@googlegroups.com
One approach, on your application_controller.rb:

rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found

  private

  # Automatically respond with 404 for ActiveRecord::RecordNotFound
  def record_not_found
    render :file => File.join(RAILS_ROOT, 'public', '404.html'), :status => 404
  end

With this approach, you need a good looking 404, but not specific.


--

You received this message because you are subscribed to the Google Groups "Philippine Ruby Users Group (PRUG/PhRUG)" group.
To post to this group, send email to ruby...@googlegroups.com.
To unsubscribe from this group, send email to ruby-phil+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ruby-phil?hl=en.



Kates Gasis

unread,
Dec 2, 2009, 9:21:31 PM12/2/09
to ruby...@googlegroups.com
AFAIK, this are one of those problems only your app goals and
requirement can decide. I used to be a big fan of 'rescue_from
ErrorClassHere'. We can only go so far into being DRY at the end of
the day it's what the app can deliver that matters.

If you're building business/enterprise apps in contrast to programming
tools, i won't suggest being too anal with DRY. Rescuing from specific
controller is fine i guess.

On Thu, Dec 3, 2009 at 10:12 AM, Katherine <bridge...@gmail.com> wrote:

Greg Moreno

unread,
Dec 2, 2009, 9:26:20 PM12/2/09
to ruby...@googlegroups.com
Hi Kath,

Alternatively, you can:

rescue_from 'ActiveRecord::RecordNotFound', :with => :not_found

private

def not_found
render :template => 'errors/not_found'
end

It depends on the page. For public pages and if you are concerned with
SEO, I suggest giving users an alternative page or suggestions, or a
search box. For pages that require authentication, a simple error
message would often suffice.

Regards,

Greg
> --
>
> You received this message because you are subscribed to the Google Groups "Philippine Ruby Users Group (PRUG/PhRUG)" group.
> To post to this group, send email to ruby...@googlegroups.com.
> To unsubscribe from this group, send email to ruby-phil+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/ruby-phil?hl=en.
>
>
>



--
Confessions of a Chief Home Officer
http://gregmoreno.ca

I'm on Twitter too!
http://twitter.com/gregmoreno

Richard Gonzales

unread,
Dec 2, 2009, 9:32:33 PM12/2/09
to ruby...@googlegroups.com
+1 on Sir Greg's suggestion.

I haven't dig that far on ActiveRecord::RecordNotFound,
but if the specific controller is passed then you can really customize your messages.

I donno you can catch params[:controller] and params[:action] before firing, that way
you could be very specific (if its possible, haven't tried it yet).

Katherine Giron Pe

unread,
Dec 2, 2009, 10:33:32 PM12/2/09
to ruby...@googlegroups.com
Thanks all...
That was more concise..

Followed Greg's suggestion, I am redirecting to a search page.

Salamat.
-----
BridgeUtopia Web
http://blog.bridgeutopiaweb.com
Reply all
Reply to author
Forward
0 new messages