0

I'm parsing the URL in Rails controller using URI.parse, i.e.

URI.parse(user_URL)

When the user inputs something such as %, the webpage raises the error

bad URI(is not URI?): %

and shows error page.

Rather, I'd like it to go to my page which says "Your URL % is not valid". Is it possible?

1 Answer 1

2

Just wrap the parse call in a begin/end block with a rescue for the case where the URL is invalid:

begin
  URI.parse(user_URL)
  ... do some stuff ...
rescue URI::InvalidURIError
  redirect_to :invalid_url_page, :url => user_URL
end

Then in your invalid_url_page controller action (or whatever you call that action), you can access the invalid url with params[:url]. In the view associated with that action, you can display the message "Your URL % is not valid" with:

Your URL <%= params[:url] %> is not valid.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.