Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7160551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:25:03+00:00 2026-05-28T13:25:03+00:00

if someone messes with the :id on a url, for example /edit/15 and id

  • 0

if someone messes with the :id on a url, for example /edit/15 and id 15 does not exist, how can I display my own “page does not exist” error page instead of processing the view (which would throw an error since it’s expecting data)

I know how to add code to check that method-by-method, but is there some global way to trap all such errors within a controller regardless of the method?

I’d like to avoid having to copy/paste something like

redirect_to "/invalid_page.html" and return if @something.nil?

in each and every method where I do

@something = MyModel.find(params[:id])

Of course if it’s a totally bogus url, such as myapp.com/nosuchpage, then the /public/404.html page gets shown. But if it’s a valid route, but an invalid ID, it throws an exception down in the view where the view is unexpectedly dealing with nil data and I have no idea if there’s a controller-wide place to trap and handle that exception.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:25:04+00:00Added an answer on May 28, 2026 at 1:25 pm

    take a look at rescue_from in the rails api docs. if you use find to load your ActiveRecord instances, it will throw an ActiveRecord::RecordNotFound exception if there is no record with the given id. you can tell your controller to rescue this exception and call a method/action instead, like this:

    class UsersController < ....
      rescue_from ActiveRecord::RecordNotFound, :with => render_404
    
    
      def render_404
        render :status => 404, :text => 'not found'
      end
    end
    

    if you’re on Mongoid you’ll have to use Mongoid::Errors::DocumentNotFound. if you use a custom finder, make sure it raises the appropriate exception.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Someone can please answer to me, if have any Editor or Compiler to C
Someone has an answer to this missing feature in Entity Framework. Does anyone have
I'm hoping someone can point me in the right direction. I'm trying to modify
This doesn't make much sense to me, and I'm hoping someone can shed some
So, I as I understand, my problem is that file_get_contents() does not send proper
Can someone please help me figure out why my accordion script at http://www.mincovlaw.com/services/copyright gets
Someone please help. I have an interesting issue. I am trying to implement an
Someone once showed me a cool IntelliJ Idea plugin that would show all the
Someone asked the question: INT, BIGINT or UUID/GUID in Oracle, DB2, Derby and HSQLDB?
Someone knows how to put an icon in a eVB 3 application, because i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.