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?
Related
I have a search page where I update the URL params on the page as filters are added or removed by the user. This allows me to deep link into the page (ie. going to /search?location=new+york&time=afternoon will set the location and afternoon filters).
I also have a filter named format. I noticed that passing in ?format=whatevervalue to the URL and then reloading the page with that param causes Rails to return a Completed 406 Not Acceptable error. It seems that format is a reserved Rails URL parameter.
Is there anyway to unreserve this parameter name for a particular endpoint?
In the context of an URL in Ruby on Rails there are at least four reserved parameter names: controller, method, id, format.
You cannot use these keys for anything else than for their intended purpose.
If you try to you will override the value internally set by Rails. In your example by setting ?format=whatevervalue you override the default format (html) and your application will try to find and render a whatevervalue template instead of the html formatted template. This will obviously not work.
Fun fact: Instead of using the default Rails path format like /users/123/edit you could use query parameters instead like this: /?controller=users&id=123&method=edit&format&html.
My suggestion is: Do not try to fight Rails conventions. Whenever you try to work around basic Rails conventions it will hurt you later on because it makes updates more difficult, common gems might break, unexpected side-effects will happen. Just use another name for that parameter.
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
I need to make a search form, where the back end used is ruby and front end is angular. the search query is generated in angular in json format and is passed to ruby via restangular service.
Its working fine. But when we tested the search string with semicolon it returned 500 Internal error. The first line puts params[:search] gives {"content":"my search is for the actual json string {"content":"my search is ; and :", "options":[]}
Please help me on how to handle it ";" also please let me know what all characters I need to be handled.
You need to encode your params first.
So the encoded params will be:
{"content":"my search is %3B and :", "options":[]}
The following is also valid:
%7B%22content%22%3A%22my+search+is+%3B+and+%3A%22%2C+%22options%22%3A%5B%5D%7D
You can encode URL using: https://www.w3schools.com/tags/ref_urlencode.asp
I am sure Angular will must have some library to encode the URLs.
You could also use native Javascript URL encoder.
The similar question was already answered on SO.
I'm reading the book, HTTP - The Definitive Guide, from which I get the URL general format:
<scheme>://<user>:<password>#<host>:<port>/<path>;<params>?<query>#<frag>
The <params> part said,
The path component for HTTP URLs can be broken into path segments. Each segment can have its own params. For example:
http://www.joes-hardware.com/hammers;sale=false/index.html;graphics=true
In my opinion, path params can also be used to query resources like query strings, but why it's barely seen?
And I'm a Rails developer, and I haven't seen its usage or specification in Rails. Does Rails not support it?
You ask several questions
Why do we not see ;params=value much?
Because query parameters using ?=& are widely supported, like in PHP, .net, ruby etc.. with convenient functions like $_GET[].
While params delimited by ; or , do not have these convenient helper functions. You do encounter them at Rest api's, where they are used in the htaccess or the controller to get relevant parameters.
Does Ruby support params delimited with ;?
Once you obtain the current url, you can get all parameters with a simple regex call. This is also why they are used in htaccess files, because they are easily regexed (is that a word?).
Both parameter passing structures are valid and can be used, the only clear reason why one is used more often than the other is because of preference and support in the different languages.
How do I pass array of parameters through Get method in rails? Currently my URL loocs like this:
http://localhost:3000/jobs/1017/editing_job_suites/1017/editing_member_jobs/new?ids[]=1025&ids[]=1027
How can I pass the array with Get method but avoid ?ids[]=1025&ids[]=1027 part.
Request is being sent with javascript window.open method. Is there any workaround to send not ajax Post request.
You should stick to using a GET request if you are not changing the state of anything, and all you want to to access a read only value.
To send an array of ids to rails in a GET request simply name your variable with square brackets at the end.
//angular snippet
$http.(method:'GET',
...
params: {'channel_id':2, 'product_ids[]': productIds}
//where productIds is an array of integers
...
)
Do not concatenate your ids as a comma separated list, just pass them individually redundantly. So in the url it would look something like this:
?channel_id=2&product_ids[]=6900&product_ids[]=6901
url encoded it will actually be more like this:
?channel_id=2&product_ids%5B%5D=6900&product_ids%5B%5D=6901
Rails will separate this back out for you.
Parameters: {"channel_id"=>"2", "product_ids"=>["6900", "6901"]}
No, GET can only put variables on the url itself. If you want the URL to be shorter, you have to POST. That's a limitation feature of HTTP, not Rails.
I recently wanted to do this and found a workaround that is a little less complex, and so has some complexity limitations but may also be easier to implement. Can't speak to security, etc.
If you pass your array values as a string with a delimiter, e.g.
http://example.com/controller?job_ids=2342,2354,25245
Then you can take the result and parse it back to what you want:
job_ids = params[:job_ids].split(',')
Then do whatever:
job_ids.each do |job_id|
job = Job.find(job_id.to_i)
end
etc
#Homan answer is valid for using an external client (e.g curl or angular). Inside Rails test cases though, you should not use []. Here's an example:
get get_test_cases_url(**path_params), params: {case_ids: ["NON-9450", "NON-12345", "NON-9361"]}
This is using new format where get_test_cases is name of route and you pass to the method all params needed to construct the URL path. Query params are a separate hash.
FYI if I use [] like case_ids[], then instead of ["NON-9450", "NON-12345", "NON-9361"] I'm getting it parsed to [["NON-9450"], ["NON-12345"], ["NON-9361"]]