Vijikumar M
Vijikumar M

Reputation: 3764

Is there any way to encode the URL at runtime in rails?

In my rails application, I have search functionality. A user may directly enter a search string in the URL. If the user enters a search string as '%', the URL becomes:

http://localhost:3000/search/% 

and that produces Bad Request error. Is there any option to encode the URL at runtime?

Upvotes: 0

Views: 50

Answers (1)

smathy
smathy

Reputation: 27961

The question you're asking wouldn't actually solve the problem you're describing. Yes, Rails can encode a URL at runtime, in fact it encodes many URLs at runtime in its normal operation.

But, that won't help you, basically what's happening is that when your users create a URL with a % in it, they're actually creating an invalid URL - not just for Rails, but for any web server, or web application server or framework.

If you look closely at the error that you get returned in the browser, it's not even from Rails, it's from WEBrick (or whatever httpd you're using), same with the error that will be logged to your logs, it's not a normal Rails error in the Routing or elsewhere.

The upshot of all this is that no, you can't handle this in Rails because in many cases it won't even get through to Rails, and it's just a totally invalid URL.

Upvotes: 1

Related Questions