How would I validate a URL?

I added categories to my Etsy-like Marketplace, how do I make it so the user can't just typing in listings?category=BLAHBKLAHBLAH (which throws an error)

Posted over 5 years ago by Mike
Posted over 5 years ago by Alex Yang

You can set up a custom page to display to users when they encounter a specific error. For example, you could create a page for the 404 error code ('Page Not Found').

1) In your config/routes.rb file, set up a new route for this error page:
get "/404", :to => "errors#not_found"

2) Create a new ErrorsController with a 'not_found' action (notice how this matches with our routes.rb line):
rails generate Errors not_found

3) Create a corresponding view page under app/views/errors called 'not_found.html.erb' and write your own HTML here (this is where you design the page)

4) In your new app/controllers/errors_controller.rb file, under the 'not_found' action, add the following code, which will still send a 404 error code along with your custom page

5) In your config/application.rb file, add the following line which tells your app to look in your routes.rb file when there's an error:
config.exceptions_app = self.routes

6) You should also add the below line in your config/environments/development.rb file if you want to test this new page in localhost:
config.consider_all_requests_local = false

Try it out by re-visiting listings?category=BLAHBLAHBLAH! Let me know how it goes.

Posted over 5 years ago by Alex Yang

One more note, if you're seeing conflicts, you may need to remove the 404.html file in your 'public' folder.