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.
Related
I want to verify the order of a URL that is generated from faceted navigation on a development website.
EG http://EcomWebsite.com/region/s/element1/element2/element3
As part of a different test step I have extracted the strings I needed and I can test these are present in the url. I've been doing this as
assertThat(displayShopPage.getCurrentURL(),containsString(displayShopPage.element1()));
assertThat(displayShopPage.getCurrentURL(),containsString(displayShopPage.element2()));
assertThat(displayShopPage.getCurrentURL(),containsString(displayShopPage.element3()));
but I mention I need to check the order of the URL.
The step in question is
"Then the order in which the values are included in the URL must be same order the values have within the facet"
I'm still rather new and raw to Java, Selenium, and BDD so I thank you for any help you peeps can provide.
EDIT:
I'm trying to avoiding hard coded url's and what I want to check in the URL for several reasons.
Element1 etc come from class's that extract the text from faceted navigation element options.
So if I hard code the URL or the text from the element options if the product data changes and so the faceted navigation it will break the test.
Hence why I'm extracting the string from the elements to compare against the URL rather than just checking for the URL it self.
getCurrentURL() returns a string, right? Why not simply compose the expected URL as a string then compare the strings?
currentUrl = displayShopPage.getCurrentURL()
expectedUrl = "http://EcomWebsite.com/" + displayShopPage.CF_PanelText() + "/s/" + displayShopPage.FacetGroup2OptionPanelText() # compose this from your page source...it isn't clear above what the component order needs to be
assertEquals(currentUrl, expectedUrl) # I made this up, I don't know the proper syntax for comparing strings
I am making a get request and I wonder if its normal that in the case that no search input was provided, I am still getting a response with random data? Making a get request to pixabay:
var API_KEY = '10677334-f0f7afbe90e1a76e6ab8380d7';
var URL = "https://pixabay.com/api/?key="+API_KEY+"&q="+encodeURIComponent('red roses'); //returns data as expected
var URL = "https://pixabay.com/api/?key="+API_KEY+"&q="+encodeURIComponent(''); //returns random data
I checked the network and in the case of empty string param, Im getting q=%20 for empty string. If I did JSON.stringify that it would somehow work (=not getting results) but my empty string turns into
q=%22%22 which does not seem right
Could anyone help and explain if I actually need to do something for that case or am I doing something completely wrong (it seems like that as strigifying etc does not seem like a typical solution) / shouldnt q= (with nothing provided in case of an empty string actually result in NOT giving me any data back?
Thanks a lot!!
This is probably a conscious decision on the part of the API developer.
For an answer to this, it's best to refer to the pixabay.com API guide.
Note, regarding the 'q' param:
A URL encoded search term. If omitted, all images are returned
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 the server programmed in Cherrypy and I use also Mako Template.
And I have the variable dict (variable Mako that contain information's work) for working with the user( this I have to use Mako and JAvascript).
I have one problem that I can not pass the value's Mako to Javascript.
MAKO --->>> JAVASCRIPT and vicecersa Not can to pass.
When the user wants change the information, I need to use the form.
The information is for example the data is the identifying a person.
When I connect when the server localhost:8100 and I have in automatically dict on Url.
The user pushes the button's send.(submit) in case of change.
The server receipt the value in Javascript with the separator in Js and the old in MAko.
I have the problem for read and to convert the separator in Javascript.
It possible to change the string's submit's form While or before to sending?
I want to program the submit's form because I want to use the other delimiter(not & and =).
This is possible?
Now I write one example:
www.theuser.com/?Name=IBM&surname=PC
With if the function programmable while sending
www.thepc.com/?Name%24+IBM+%23%+Surname%24+PC
Repeat: when I sent the parameter, I not want this separator & or = and I want to use the others.
Separator
javascript Mako
= %24+
& +%23+
This Query String is the original for the my project:
http://localhost:8100/index2?json_data=demo_title%24+Demo+title+%23+proc1_script%24+script.sh+parameters+%23+proc1_chk_make%24+on+%23+outputp2_value%24++%23+demo_input_description%24+hola+mundo+%23+outputp4_visible%24+on+%23+outputp4_info%24++%23+inputdata1_max_pixels%24+1024000+%23+tag%24++%23+outputp1_id%24+nanana+%23+proc1_src_compresion%24+zip+%23+proc1_chk_cmake%24+off+%23+outputp3_description%24++%23+outputp3_value%24++%23+inputdata1_description%24+input+data+description+%23+inputp2_description%24+bien%3F+%23+inputp3_description%24+funciona+%23+proc1_cmake%24+-D+CMAKE_BUILD_TYPE%3Astring%3DRelease++%23+outputp2_visible%24+on+%23+outputp3_visible%24+on+%23+outputp1_type%24+header+%23+inputp1_type%24+text+%23+demo_params_description%24+va+bien+%23+outputp1_description%24++%23+inputdata1_type%24+image2d+%23+proc1_chk_script%24+off+%23+demo_result_description%24+win%3F+%23+outputp2_id%24+nanfdsvfa+%23+inputp1_description%24+funciona+%23+demo_wait_description%24+boh+%23+outputp4_description%24++%23+inputp2_type%24+integer+%23+inputp2_id%24+papapa+%23+outputp1_value%24++%23+outputp3_id%24+nananartrtrt+%23+inputp3_id%24+pepepe+%23+outputp3_type%24+header+%23+inputp3_visible%24++off+%23+outputp1_visible%24+on+%23+inputdata1_id%24+id_lsd+%23+outputp4_value%24++%23+inputp2_visible%24+on+%23+proc1_source%24+lsd-1.5.zip+%23+inputp3_value%24+si+%23+proc1_make%24+-j4+-C++%23+images_config_file%24+cfgmydemo.cfg+%23+outputp2_type%24+header+%23+proc1_subdir%24+xxx-1.5+%23+proc1_url%24+http%3A%2F%2Fwww.ipol.im%2Fpub%2Falgo%2F...+%23+inputdata1_image_depth%24+1x8i+%23+inputp1_id%24+popopo+%23+inputp1_value%24+si+%23+inputp2_value%24+no+%23+demo_data_filename%24+data_saved.cfg+%23+inputdata1_info%24+info_lsd+%23+outputp3_info%24++%23+inputdata1_image_format%24+.pgm+%23+outputp1_info%24++%23+inputdata1_compress%24+False+%23+inputp1_visible%24+on+%23+proc1_id%24+lsd+%23+outputp4_id%24+nana+%23+outputp2_description%24++%23+outputp4_type%24+header+%23+outputp2_info%24++%23+inputp3_type%24+float+%23+&tag=&inputp4_id=hi&inputp4_type=text&inputp4_description=hello+program&inputp4_value=no&inputp4_info=bol&inputp4_visible=on
For the moderator:
I read on the post https://stackoverflow.com/questions/13353539/how-to-change-how-the-url-is-encoded-when-a-form-is-submitted
But this was not interested in me.
P.s. The solution in Jquery or Javascript is equal for me.
Well I'm pretty sure your reasons for doing this don't justify doing it, but to answer the question, this is how you would change the tokens. I'm assuming jQuery, it's not entirely necessary but makes the code shorter.
HTML:
<form id="myform" action="myparser.php">
<input ...>
</form>
JavaScript:
$('#myform').submit(function(e){
e.preventDefault();
var q=$(this).attr('action'),f=this.elements,i;
for(i=0;i<f.length;++i){
q+=(i===0?'?':'+%23%+')+f[i].name+'%24'+f[i].value;
}
document.location.href=q;
return false;
});
That's slightly minified, so here's the gist. We begin by binding to the submit event, which we prevent (preventDefault and return false to be doubly sure), then get all the form's elements (this.elements) and iterate through them. By the end of the loop, q is a full URL which we want to submit to (using the action property and filling in all the names/values), so we just set the HREF to it and off we go. In this case to myparser.php.
Note that this does no character substitution whatsoever. You should make f[i].value safe in some way. From your question, it seems obvious that you don't want standard URL encoding, but you will need to do something to prevent bad characters being used.
Finally, this is just the sending side. You'll still need to do something clever on your server-side to actually read these values.
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].