0

I want to return the same generic invalid page if a user does not have access to a page. I want to make use of this invalid page from several different views.

How can I render this page without creating them in every view?

Yes, I can create some other controller and redirect to those pages, but I want the url in the browser to be the same as the page the person is trying to access. I want to render the page not redirect.

Mars
  • 4,677
  • 8
  • 43
  • 65

1 Answers1

1

You're going to create a static page, which we'll call invalid.html.erb and place it in your static_pages directory (or however you maintain static pages for your app). Make sure you create a route for it in routes.rb as well:

get 'static_pages/invalid' => 'static_pages#invalid'

Then, in your application_controller.rb file, you're going to use the rescue_from helper to call a custom method to handle the error:

rescue_from User::NotAuthorized, :with => :invalid

def invalid
  redirect_to 'static_pages_invalid_path'
end

That's pretty much it. Read more at Ruby Guides.

MarsAtomic
  • 10,436
  • 5
  • 35
  • 56