I'm in the following situation. The current url looks like the following:
/categories/Art
And I'm using name = location.pathname.split('/')[2] in order to grab the Art portion of the URL. Then, I send an AJAX the following path back to the controller: http://localhost:3000/sort?sortMethod=name&category=name or date, whichever link is clicked on.
Now in my controller I can use sort = params[:category] to get the string name, yet what I'd like to do with this string is sort an array by it. #categories is an array of objects and I'd like to call .sort_by(&:sort) yet it doesn't recognize the string value of sort = name. So now I'd like to convert this string into a proc in order to sort the array. Anyone know how I accomplish this?
Any help is greatly appreciated!
Convert it to a symbol first and then use Symbol#to_proc:
#categories.sort_by(&sort.to_sym)
However be sure that the users can't call anything malicious on your objects like:
http://localhost:3000/sort?sortMethod=destroy
One way of protecting yourself is to use attribute_accessible definitions in your model and then do
#categories.sort_by(&sort.to_sym) if Category.accessible_attributes.include? sort.to_sym
The problem is not entirely clear, so you may need to adjust the following solution. However, the basic idea is taht it's possible to do something like:
Category.all.sort_by {|category| category.method(params[:category]).call }
Also, the "Art" portion of your url is available in Rails' params hash as params[:action], so you don't have to do location.pathname.split('/')[2].
Related
So I am using a plugin for tags in a form and it works well but I am having troubles with the suggestions.
You can have an autocompleter by specifying an attribute
suggestions: ['Black','White','Red','Blue','Green','Orange']
Thing is, I have to make a call to a servlet to find the keywords to put in there, so I end up with a String that I try to pass to the attribute.
var dataFromServlet = "'Black','White','Red','Blue','Green','Orange'";
suggestions: [dataFromServlet]
Thing is, now the completer assumes the ENTIRE String is one element.
I have been at this for hours and I can't for the life of me figure out how to solve this seemingly very simple problem. I have tried using arrays, passing JSON from the servlet instead and parsing it, splitting the string and using a loop to rebuild it differently, nothing works. This is driving me crazy, I feel like I'm missing something very obvious.
The plugin documentation does suggest a different method for fetching suggestions via ajax calls, but it ends up calling the ajax method on every single keypress and it lags the whole thing out.
The best solution, have the backend return a proper JSON object. That way you do not have to worry about your hacky solution breaking because of the data in the string.
If for some problem out of your control you can not make a change to the back end, you can convert to an array by reformatting the string and using JSON.parse
var dataFromServlet = "'Black','White','Red','Blue','Green','Orange'";
var obj = {
suggestions: JSON.parse("[" + dataFromServlet.replace(/'/g, '"') + "]")
};
console.log(obj.suggestions);
Again, this solution is a bandaid, the correct solution is make the back end servlet return proper JSON formatted data.
I'm trying to use the ES6 String template to query my database
My SQL is like :
SELECT * FROM table WHERE id IN ();
The in must contains each items of an array, so, i tried something like this :
return connector.query`SELECT * FROM table WHERE AnalogHistory.TagName IN (${sensor})`.then(result => {return result.recordset});
This doesn't work. But if i try something like this :
return connector.query`SELECT * FROM table WHERE AnalogHistory.TagName IN (${sensor[0]},${sensor[1]}, ${sensor[2]}, ...)`.then(result => {return result.recordset});
Well this time, it work. So do you guys know what differ and on to do it by the first way (the cleaner) ?
We can't be absolutely sure without knowing how query handles the tag parameters, but the difference is that in the first case, you've provided query with the array as a single parameter; and in the second case, you've provided query with a series of individual parameters with commas in-between them.
Apparently it's happy with the second, and not with the first. Whether there's a third option that would be less laborious but also functional depends entirely on how query is designed to be called.
Re your comment saying it's the mssql npm package: I don't see anything in its documentation to suggest that it supports passing arrays into the query tag function as parameters. Which is really unfortunate.
I'm working urls returned from a server that I have no control over where and sometimes the urls return with extra data at the front.
For instance
sometimes it returns this
https://example.com/image/5119b3905.jpg
and this I can use, but sometimes it will return something like this
https://d1yww.cloudfront.net/9MA=/670x670/example.com/image/5119b3905.jpg
where I'd like to use remove everything before the example.com and to do that I could use something like lodash's _.trimStart method something like
_.trimStart('https://d1yww.cloudfront.net/9MA=/670x670/example.com/image/5119b3905.jpg',
'd1yww.cloudfront.net/9MA=/670x670');
but the d1yww.cloudfront.net/9MA=/670x670' is never static for me to do this and I don't know how to grab the dynamic data to use _.trimStart and I don't see any other useful lodash's methods and I don't know of any vanilla javascript ones.
TLDR: How can I remove dynamic data in string before a value in that string (in this example everything before the example.com)
You don't need lodash to do that
var str = 'https://d1yww.cloudfront.net/9MA=/670x670/example.com/image/5119b3905.jpg'
str.substr(str.indexOf('example.com'))
You could search for a Regular Expression
For Example:
/\/([-a-zA-Z0-9#:%._\+~#=]{2,256}\.[a-z]{2,6}\b)\//g
and look for the second match
I have an MVC application where I am passing some arguments via GET. Everything works great but some of the information I am passing I would like to keep the case of. For example one field I pass is title, so ?title=My Title that works great, except when I get to my controller method the value in the title field of my model is my title is there any way I can keep the case calling a controller in this manner?
This is how I am calling the controller in javascript like so:
var url = "#(Url.Content("~/Controller/Name/?title=My Title"))";
window.open(url, "_blank");
First make this function...
function firstCharsToUpperCase( char ){
return char[0].toUpperCase();
}
Then in your controller you should have access to params or a key/value pair of some sort like this...
params['title'] = params['title'].replace(/\b[a-z]/ig, firstCharsToUpperCase);
You would then reassign the key title to the new value of "My Title"
Also, params should be case insensitive. Perhaps there is something converting the URI?
Maybe its in the Url.content() function. Can we see what it does?
Also, here is an article regarding case sensitive routes Never Use Capital Letters
UPDATE
I found this
If the specified content path does not start with the tilde (~) character, this method returns contentPath unchanged.
here link
Perhaps what it is saying is that if the '~' is left off, you will get what you give it. Try it out and see?
Case sensitive URLs are not a good idea even when not causing SEO issue, its just a bad idea. I developed a solution where I passed an ID in the URL and queried the necessary data from the DB within my controller.
I am trying to create a fairly complex system for my website. I want to be able to write some pseudo like code and then parse it to make it do something in my back-end.
My data is inside two $.each loops as this is an Object of data with multiple levels to it.
For instance, I want to take a string like this:
"<!this!> == <!PropertyStreetNumber!>"
Then how I would like for the above code to executed is this:
FormData[parentKey][this] == FormData[parentKey]["PropertyStreetNumber"]
Thanks for any help!
Here's some of my code, the code where this would need to go in (see commented area)
http://jsbin.com/liquvetapibu/1/
Is there any restriction not to use regular expressions on JavaScript?
You could do something like this:
var myString = "<!this!> == <!PropertyStreetNumber!>";
var aux = /<!(.*?)!> == <!(.*?)!>/.exec(myString);
The value of aux will be an array with 3 elements:
The string that was tested.
The first element within <! !>
The second element within <! !>
Then it would depend on what the content on each one is: in your example this is an object, while you seem to use PropertyStreetNumber as a string (maybe a typo?). If you want to use it as an object, you will have to use eval() (e.g.: eval(aux[1])) while if you want to use it as a string, you can use it directly (e.g.: aux[2]).
Conceptually, the first thing you would need to do is determine the type of statement you are working with. In this case, a comparison statement. So you need a regex statement to filter this into a "statement type".
Once you do that, you can figure out what the arguments are. So you create a regex to pull out the arguments on each side of the operator.
Next, the strings that represent action code items need to be parsed. The this argument is actually an object, whereas "PropertyStreetNumber" is a string. You've got to be able to determine which is which. Then you can filter that into a function that has been created specifically to handle those statements types.
If at all possible, I would try to avoid the use of eval(). You can get into trouble with it.
you could try with
var beg = str.indexOf("== <!") + 5;
to find the index of the beggining and then slice counting the chars from beginning like
str.slice(beg, -2);
and from there build the rest.
couldnt that work?`