Rails wildcard route with full URL in param - ruby-on-rails

I want to redirect to any given URL. I am trying this:
Route:
get 'track/*redirect_url', to: 'tracker#track'
Controller:
redirect_to params[:redirect_url]
However when I visit tracker URLs - I am getting strange redirects:
http://localhost:3000/http://google.com/search=xyz => http:/google.com/search=xyz (one slash is missing!)
http://localhost:3000/http://google.com => http:/google (slash + .com is missing"
Rails is apparently somehow transforming URL parts but since I do not have a control over it - I need a way to fix it.
Any ideas how?

It happens because rails tries to parse that as a single url and fails because of escaping (// is parsed as "escaped /").
Also what happens to .com - it's parsed by rails as format (like .html or .json).
I would suggest you doing it with URL encoding and query-string params, eg:
http://localhost:3000/track/?redirect_url=https%3A%2F%2Fgoogle.com

Related

Alter the browser URL in Rails

Is there any way to change the browser URL of the request?
Lets say I've a '/articles/foo-bar' and I need to change this to '/articles/foo_bar'.
I've tried Routing Constraints and changed the request.path_info with no luck. I successfully changed the parameters but is not reflected in Browser URL.
PS: I want to replace '-' to '_' in browser URL in overall App.
EDITED:
Routes: get 'brands/:name-:id/articles', to: 'brands#articles'
Here name can be changed dynamically. And if a dash in name should be replaced by underscore.
You can use to_params method on your model
Please check this
http://blog.teamtreehouse.com/creating-vanity-urls-in-rails

Match double slash in rails route constraint

Suppose I am expecting a url as part of my route - maybe a callback url or similar - I might use the following route:
get '/mymodel/:url', to: 'mycontroller#docallback', url: /.*/
Now I would like to be able to go to http://www.myapp.com/mymodel/http://www.google.co.uk/ and process http://www.google.co.uk/ in mycontroller - but it is processed as http:/www.google.co.uk/ (one slash). How can I rectify this? Is the regex wrong or is there some flag I have to set?
I don't think that "http://www.myapp.com/mymodel/http://www.google.co.uk/" is a valid url.
Normally if you want to pass a url as a parameter you would call CGI.escape on it first, which would convert "http://www.google.co.uk/" to "http%3A%2F%2Fwww.google.co.uk%2F" CGI.escape will turn any string into a url-safe version of itself, basically replacing any characters which have a special function in a url, like ":/?&" and also space and some other characters which would otherwise break the formatting.
So, you would end up with a url like
"http://www.myapp.com/mymodel/http%3A%2F%2Fwww.google.co.uk%2F"
which would come through in params like
params = {:url => "http://www.google.co.uk/"}
Note how it's been unescaped here: Rails automatically* calls CGI.unescape on parameter values before putting them into the params hash.
However, this url
"http://www.myapp.com/mymodel/http%3A%2F%2Fwww.google.co.uk%2F"
looks pretty weird to me. It would be better to be more explicit and pass it through as a named parameter in the url itself, like
"http://www.myapp.com/mymodel?url=http%3A%2F%2Fwww.google.co.uk%2F"
which will require a slight change to your routes.
* I think Rails will do this but it might depend on circumstances. Try it.
...Turned out that the request was not encoded on the client side before being sent, solution was to use encodeURIComponent() on the url before sending it.

How to get rails to parse a url with a hashbang?

I have an incoming url that looks like this (due to my AngularJS configuration):
/new_query#?query=somequery
If the hashbang (#) hadn't been there, Rails would parse it correctly, and extract the query parameter into the params variable. However, it doesn't seem to successfully extract the params when the hashbang is there.
Is there any standard way / config to solve this?
Or would I have to monkey patch some parameter parsing code in rails, by i.e. making a regular expression to find the # and remove it from the url string?

Tapestry "/" in parameters bug

If I add slash / to the parameter of a page, even in encoded form %2F I get an error.
Sample URL:
http://mywebsite.com/somepage?param=dfgdfg%2F
Error:
Input string 'dfgdfg/' is not valid; the character '/' at position 7 is not valid.
I am trying to pass whole URL as parameter (to later redirect user to that URL) so there are a lot of slashes in there.
Is this a bug? Is there any workaround?
I could theoretically replace all slashes with something else than %2F but that is something I would attempt after everything else fails...
As I've learned this happens on Jetty only, which I use for development...
This custom service override solved the problem:
http://tapestry.1045711.n5.nabble.com/Customising-T5-URL-Encoding-tp2412550p2412551.html
Looks very hacky but works :).
See the Web Services More example on the JumpStart page. Works for me on Jetty.
The JumpStart page has many "how-to-do-this-in-Tapestry" examples.

How send parameters with decimal point in url?

I’m using Backbone.js and Rails.
In Backbone.js I use HTML5 push state to set filter parameters in a url.
When the page is reloading I want to pass these parameters to Rails.
I encoded a parameter lat:34.34+lng:45.23 using JavaScript’s encodeURIComponent. It encoded:
/users/nearby/lat:34.34+lng:45.23/
as:
/users/nearby/lat%3A34.34%2Blng%3A45.23
but this route is not found.
If I delete the points from url, it works.
How can I send parameters with a decimal point?
The . is not a character that has to be encoded. Is this causing issues server side?
See here for more details:
http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
I solved my problems adding an "extra" slash to the end of the url.
In your encoded url it is missing.
Hope this helps
One manifestation of this problem is in URL pattern matching, where the Query Param is expected (i.e. matched) to be an integer. This does not match a number with a decimal point. So you get a 404 (URL not found).

Resources