Change Value of Instance Variable in Ruby - javascript

I have an instance variable in my controller and would like to increment it's value with Javascript. Am I able to do this? If so, how and where do I put this Javascript file?
A simple counter is all that I'm looking for. I'm not sure if I should look into calling AJAX instead. I need this to be done on the client-side without any page refreshes.

Yes, you will have to use Ajax to update your variable in controller. But I'm not sure how flexible an instance variable for this.
But have your variable in the session instead. Simple work flow will be:
you have a session variable
session[:counter] = 0
you have the increment code in the controller
def increment_session
session[:counter] += 1
end
and you will call the 'increment_session' via, AJAX from your view
assuming you are using Rails < 3, the following are some useful links for implementing AJAX call:
http://railsforum.com/viewtopic.php?id=19606
http://www.ibm.com/developerworks/java/library/j-cb12056/
http://apidock.com/rails/ActionView/Helpers/PrototypeHelper/link_to_remote

Related

Why does my JMeter While Controller Loop Infinitely?

I'm trying to set up a JMeter run that does this:
Make Rest API request
Use a JSON Extractor to check the response for a given array of values.
I define success as all of the "Items[*].Success" nodes equaling "true"
If the response is successful, break out of the loop (continue to step #5)
If the response is a failure, go back to step #1
... the rest of my test steps
Here's what I've set up to do this:
Use a BeanShell Assertion to initialize a loop variable:
${__setProperty(is_any_calc_pending,true)};
While Controller with a condition set to
${__BeanShell(props.get("is_any_calc_pending")}
My problem is here, the loop never stops
Make Rest API call (This works as expected)
JSON Extractor (This also works as expected)
Names of created variables = api_successes
JSON Path expressions = $.Items[*].Success
Match No. = -1
Compute concatenation = checked
Default Values = unset_api_successes
JSR223 PostProcessor set to Javascript to update the value of the loop variable.
Here's my code for step #5. It simply checks whether or not there's a "false" in the api_successes_ALL variable that the JSON Extractor creates.
var api_successes_ALL = vars.get('api_successes_ALL')
var all_successful = api_successes_ALL.indexOf('false') < 0
props.put('is_any_calc_pending',!all_successful)
Most of this works as I expect; I can check this using the Debug Sampler. The problem I'm having is that the loop never stops. The condition never causes the loop to break.
In the log, I see this line:
DEBUG o.a.j.c.WhileController: Condition value: 'false'
The documentation says that the While Controller will continue until the condition is false. From what I see in the log, the condition is always false. I also don't understand why the While Controller never sees that the value of my is_any_calc_pending changes. I can see in the Debug Sampler that the value changes.
Is the variable being re-initialized somehow? I'm wondering if my variable or property is going out of scope.
I got it. Here's what I did:
Simple Controller (I'm not sure if this is necessary)
Make Rest API call to initiate process
While Controller, see condition code below
Make Rest API call to check status
JSON Extractor with the same property values I listed in my question
I didn't expect it, but the While Controller has access to the variables generated in the JSON Extractor. I think the Simple Controller may have caused this.
While Controller condition. I left the log.warn() call in there to show how I debugged the condition.
${__javaScript(
log.warn( vars.get("api_successes_ALL") );
!!vars.get("api_successes_ALL") ?
(vars.get("api_successes_ALL").indexOf("false") >= 0) :
"true";
)}
Found it...
The JSON PostProcessor has to be a child of the Request, not of the While Loop.
Lost a few hours on this.
WhileLoop
HTTP Request
JSON extractor
Delay

What is the correct way to capture variables for direct call rules?

What is the correct way to declare variables for direct call rules so that I can capture it in DTM?
For example I have a page where I want to record a pageview because the DOM hasn't changed in the page so I will be using DTM to call "ajaxPage" to get the value of form_name and put it in an eVar1. Is it version 1 or version 2?
Version 1:
_satellite.track('ajaxPage');
var form_name = 'contact-search';
Version 2
var form_name = 'contact-search';
_satellite.track('ajaxPage');
Not sure I completely understand the use case but Direct Call rules are used when you need to "force" analytics to be sent on an action that cannot be captured by a typical page load rule or event-based rules (clicks, etc.)
In the case of capturing the form name on an ajax submission, once ajax has returned you can capture the form name via jQuery and call your Direct Call Rule.
Within the custom code of that rule you should then be able to set eVar 1 with the form name.
The trick is to call the direct call rule after ajax has returned and then within the custom code, grab the form name and set eVar 1.
Hope this helps.

How to pass parameter value of server-side method from JavaScript? [duplicate]

This question already has answers here:
How to pass a javascript variable to server side method
(3 answers)
Closed 7 years ago.
Here is an example:
JavaScript:
var b = 'Banana';
var list= <%= getJson() %>; // want to pass b?
C# Method:
using System.Web.Script.Serialization;
....
public string getJson(string x)
{
var list = new List<object>{new []{ "1","Apple"}, new []{ "2",x}};
return (new JavaScriptSerializer()).Serialize(list);
}
How can I pass variable b when I call getJson() from JavaScript?
The problem is, the server-side code executes and generates the page (including the javascript which is just text at this point). The page is then sent to the browser where the javascript is executed.
By that time, it's too late to do anything server-side.
Your options are:
Do a GET/POST/similar of the whole page back to the server, with the new variable from JS in a form field. This is trivial but causes a full-page refresh and is becoming less and less desirable.
Use a Javascript Ajax request to pass the variable back to the server and ask it for updated content. Use the response from the server to update the page for the user
The documentation for how to do the latter using jQuery is available here and there are literally thousands of examples around Stack Overflow.
Remember, anything called inside the <% ... %> tags get processed on the server side before the page even loads. If you need to dynamically load information from the server based on a variable, you need to make an AJAX request.
If you are able to, the easiest way to do this is with jQuery.

HTML Append Variable to Query String

I have http://localhost/?val=1
When I click on a link, is there a way this link can append a query variable to the current string, for example:
Link
so when I click it the url would be:
http://localhost/?val=1&var2=2
but when I click the link it removes the first query string and looks like
http://localhost/&var2=2
Is such a thing possible with normal HTML?
You can't do that using only html, but you can do it with js or php:
Using JS:
<a onclick="window.location+=((window.location.href.indexOf('?')+1)?'':'?')+'&var2=2'">Link</a>
Using Php:
Link
Notice 1: make sure you don't have the new variable in the current link, or it'll be a loop of the same variable
Notice 2: this is not a professional way, but it could work if you need something fast.
Basically you want to get your current URL via JavaScript with:
var existingUrl = window.location.href; // http://localhost/?val=1
Then append any Query Strings that are applicable using:
window.location.href = existingUrl + '&var2=2';
or some other similar code. Take a look at this post about Query Parameters.
Note: A link would already have to exist with an OnClick event that calls a function with the above code in it for it to work appropriately.
Now obviously this isn't very useful information on it's own, so you are going to want to do some work either in JavaScript or in Server code (through use of NodeJS, PHP, or some other server-side language) to pass those variable names and their values down so that the button can do what you are wanting it to do.
You will have to have some logic to make sure the query parameters are put in the URL correctly though. I.E. if there is only one query param it's going to look like '?var1=1' and if it's any subsequent parameter it's going to look like '&var#=#'.

Passing parameters or variable using location()

I am trying to reload a page using location() in JavaScript and would also like to pass a variable value. below step didn't work for me. Let me know the correct procedure.
location='success.jsp'+<%=ID%>;
You'll want to do something like:
window.location='success.jsp?VARIABLE_NAME='+<%=ID%>;

Categories

Resources