This is a silly question but weird enough I Googled it, I am sure i had seen it before in Rails guides but now couldn't find it.
I want to attach parameters to my URL.
My initial url is this: "http://localhost:3000/pharmacy/patients"
Now I attach one URL with string concatination in JavaScript and it will be this:
"http://localhost:3000/pharmacy/patients?provider=234"
And still good.
Now I want to attach a second parameter named thera_class and its values are strings with spaces between them like "Nasal Congestion"
If I want to also concatenate that second parameter to it, How would the URL look like?
The way it would look is:
http://localhost:3000/pharmacy/patients?provider=234&thera_class=Nasal Congestion
To be extra strict, spaces are replaced by %20 in the URL:
http://localhost:3000/pharmacy/patients?provider=234&thera_class=Nasal%20Congestion
Related
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.
I have a rails 3 app and now i implementing filter for my catalog. Filters form pass data to controller through GET request. As a result i have link like this in my browser after i submit
my form (apply search):
http://localhost:3001/shoes?filter%5BShoeBottomType%5D%5B%5D=2&filter%5BShoeClassification%5D%5B%5D=1&filter%5BShoeClassification%5D%5B%5D=2&filter%5BShoeElation%5D%5B%5D=3&filter%5BShoeElation%5D%5B%5D=4&filter%5BShoeElation%5D%5B%5D=5&filter%5BShoeLiningColor%5D%5B%5D=2&filter%5BShoeLiningColor%5D%5B%5D=3&filter%5BShoeLiningColor%5D%5B%5D=4&filter%5BShoeTopColor%5D%5B%5D=1&filter%5BShoeTopColor%5D%5B%5D=2&filter%5Bonly_action%5D%5B%5D=1&page=2
Is there a way to do URL more beautiful?
PS i dont want use POST request, because I read that it is bad for SEO
TLDR: just leave it.
HTML forms serialize in a straightforward manner; the parameters are named after the HTML elements. The actual issue here is how the form elements are named. It looks like they have names like filter[ShoeBottomType][]; look into your HTML to see the name attributes. Since you're in Rails, I'm guessing you having a filter hash passed to your Rails controller method as a single argument, and since Rails expects hashes to use a certain URL format for hashes and arrays (it has to know how to deserialize it from the request), the form helper writes the form that way. And yours is especially complicated because the hash values are arrays, hence the extra set of brackets. Then it's URL encoded and you end up with an ugly mess.
You could avoid some of this problem by passing the inputs individually back to the controller instead of as a big hash. Something like:
def index
shoe_bottom_types = params[:bottom_types]
shoe_classifications = params[:classifications]
shoe_elations = params[:elations]
...
which will get you to: /shoes?bottomTypes[]=1&bottomTypes[]=2.... That doesn't seem much better, and now your controller is all gross. And I don't see how you're going to get rid of the brackets entirely if you want to have more than one of the same filter. I guess you could get crazy and do your own parsing in your controller, like breaking apart shoeBottomTypes=1|2, but then you'll have to do your own form serialization too. Again, just not worth it.
Backing up for a sec, the SEO stuff doesn't make much sense. Search engines won't fill out your form; they just follow links. The real reason you should use GET is that (presumably), submitting your form doesn't have side effects, since it's just a search. See here; it's important to use the right HTTP methods. If you use POST, you'll get weird warnings on reloads and you won't be able to bookmark the search.
Backing up even further, why do you care, especially now that SEO is out of the picture? Just as a quick demo, I did a google search for the word "thing" and this was the URL:
https://www.google.com/#hl=en&output=search&sclient=psy-ab&q=thing&pbx=1&oq=thing&aq=f&aqi=g2g-s1g1&aql=1&gs_sm=3&gs_upl=764l1877l0l1980l6l6l0l0l0l0l89l432l5l5l0&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.,cf.osb&fp=220ef4545fdef788&biw=1920&bih=1086
So URLs for form submissions can be long. The user won't even look at it.
The only possibility I can think of for why you'd care about the length/ugliness of your URL here is that you want, separately from the form, to create links to certain searches. There are several ways to handle that, but since I don't know whether that's relevant to you, I'll let that be a follow-up.
So bottom line, it looks like I'd expect, and trying to fix it sounds ugly and pointless.
If you do not want to use a POST request, then there is no other way then to put the form values in the URL -- they have to get to the server one way or another.
On the other hand however, I do not see why doing a POST would be bad for SEO and I would love to see the article that stated so.
My suggestion is that you could add some custom routes to beautify your urls.
For example :
http://localhost:3001/shoes/Type/2/Classification/1,2/Elation/3,4,5/LiningColor/2,3,4/TopColor/1,2/only_action/1/page/2
This is far much shorter than your initial URL ;)
The counterpart is that, as far as I know, you have to use always the same order for params in your url.
The routing rule is the following :
match "shoes/Type/:type/Classification/:classification/Elation/:elation/LiningColor/:liningcolor/TopColor/:topcolor/only_action/:only_action/page/:page" => "shoes#show"
You can retrieve the passed values in params array. You have to split the string containing , in order to retrieve the multiple values.
If an extra character (like a period, comma or a bracket or even alphabets) gets accidentally added to URL on the stackoverflow.com domain, a 404 error page is not thrown. Instead, URLs self correct themselves & the user is led to the relevant webpage.
For instance, the extra 4 letters I added to the end of a valid SO URL to demonstrate this would be automatically removed when you access the below URL -
https://stackoverflow.com/questions/194812/list-of-freely-available-programming-booksasdf
I guess this has something to do with ASP.NET MVC Routing. How is this feature implemented?
Well, this is quite simple to explain I guess, even without knowing the code behind it:
The text is just candy for search engines and people reading the URL:
This URL will work as well, with the complete text removed!
The only part really important is the question ID that's also embedded in the "path".
This is because EVERYTHING after http://stackoverflow.com/questions/194812 is ignored. It is just there to make the link, if posted somewhere, if more speaking.
Internally the URL is mapped to a handler, e.g., by a rewrite, that transforms into something like: http://stackoverflow.com/questions.php?id=194812 (just an example, don't know the correct internal URL)
This also makes the URL search engine friendly, besides being more readable to humans.
I'm using playframework, and I hope to generate complex urls like stackoverflow. For example, I want to generate a question's url:
http://aaa.com/questions/123456/How-to-generator-a-complex-url
Note the last part, it's the title of the question.
But I don't know how to do it.
UPDATED
In the playframework, we can define routes in conf/routes file, and what I do is:
GET /questions/{<\d+>id} Questions.show
In this way, when we call #{Questions.show(id)} in views, it will generate:
http://aaa.com/questions/123456
But how to let the generated has a title part, is difficult.
With playframework it's easy to generate such url. In your routes file you add this :
GET /questions/{id}/{title} YourController.yourMethod
See the doc in playframework site about routing for more info
In your html page :
<a href="#{YourController.yourMethod(id,title.slugify())}">
slugify method from JavaExtensions, clean your title from reserved characters (see doc)
It a server-side url rewriter does. In case of SO it doesn't matter you type {...}/questions/4698625/how-to-generate-complex-url-like-stackoverflow or {...}/questions/4698625 - they both redirects to the same content. So this postfix is used just to increase readability of a url.
To see more details about url rewriting, see this post.
UPD:
to generate such a postfix,
take a title of the content,
shrink multiple whitespaces into single
replace all whitespaces with dash (-)
remove all non-letter symbols from a title
Better to perform this operations with Regular Expressions
I am facing a problem that one of my fields need to be shown in the url contains special character (/, \, :).
The stupid way to handle this generate action links by using UrlEncode(). Then UrlDecode is used before consuming in controller. But I think it really stupid because too many places need to be adapted.
So, my problem is there any way to extend the url route or just write my own one to achieve it?
Thanks,
Mike
You can extend the System.Web.Routing.Route object to create a custom route and override the GetRouteData and GetVirtualPath methods. These are called to resolve a route's values and create a URL from given route values, respectively. However, I don't think URLs can contain URL encoded values for / (%2f) within the path portion of a URL though it is ok in a query string.