I am trying to use a JavaScript variable in an HTML erb, using <% %> tags. query[i] is something I have in loop I am trying to call:
<%= Table.update('query[i]')%>;
How can I use query[i] from JavaScript for the actual value?
You can't do this. erb runs on the server to generate the HTML, which is then sent to the client, where the Javascript runs. By the time the Javascript variable is available, erb has already executed, and we're not even on the same machine anymore!
It looks as though you want Javascript code to supply a value which is then used as an argument to a database call. You'll have to do this via a post-back; i.e., the Javascript can make an AJAX call, a new erb could generate the new HTML fragment, and then the Javascript can receive it and plug it into the page.
Here's the first link I got when I googled "erb AJAX".
Related
I have a form with a fileupload control in it, and I call form.submit with a success function.
My server side does the usual trick of setting the content type to text/html to get it to arrive in one piece.
In the success function, action.response.responseText does contain the JSON which I sent.
When it leaves the server, it looks like:
{
html: "<div>a div</div>"
}
When it arrives in the success function, the tags are missing. What's going on? Do I need to put some sort of html cdata wrapper around the entire response on the server to avoid this?
A string in a string in JSON. As long as it is well-formed you are allowed to put HTML in string values (making sure you escape quotes etc.).
It's probably the function you're using to insert the HTML that's stripping the tags.
Here's the situation. When you ask ExtJS or JQuery to do Ajax for a form with a file upload, it has to use an iframe. For the response to come back correctly, it has to be of content type text/html whatever is in it. So it has to have it's HTML characters escaped for HTML, which I accomplished with a function from CommonsLang.
I'm trying use HTML5 localStorage with a Ruby haml template and need to be able to get the value of localStorage.getItem('myItem') to pass to a java applet (code stripped down):
- content_box("MyBox") do
%object{:classid => "clsid:xxx"}
%param{:name => "myItem", :value => "javascript:localStorage.getItem('myItem')"}
%comment
%EMBED{:myItem => "javascript:localStorage.getItem('myItem')"}
%noembed
Is there a good way to do this? I can do something like:
:javascript
document.write("<param name='myItem' value="+localStorage.getItem('myItem')+">"
but that's so ugly!
Note that this is an object I'm embedding, and need the value to be present before document_ready; I cannot select the object and append the value to it on document_ready. The only other way I can think of is to do an ajax submission to make the value a Ruby variable ahead of time, but that's really unnecessary.
Thanks!
Sometimes the only way that works is ugly.
IF your data is stored on the client, creating a server request/page/action just to get the data and pass it back in a different form straight back to the client is uncessessary, and arguably uglier.
Go with using javascript to add the <param> tag.
If the object depends on JavaScript anyway, you may as well just write the whole element with JavaScript instead of just the param. Then you can do it on document ready.
I am having trouble with some Ruby CGI.
I have a home page (index.cgi) which is a mix of HTML and Ruby, and has a login form in it.
On clicking on the Submit button the POST's action is the same page (index.cgi), at which point I check to make sure the user has entered data into the correct fields.
I have a counter which increases by 1 each time a field is left empty. If this counter is 0 I want to change the current loaded page to something like contents.html.
With this I have:
if ( errorCount > 0 )
do nothing
else
....
end
What do I need to put where I have the ....?
Unfortunately I cannot use any frameworks as this is for University coursework, so have to use base Ruby.
As for using the CGI#header method as you have suggested, I have tried using this however it is not working for me.
As mentioned my page is index.cgi. This is made of a mixture of Ruby and HTML using "here doc" statements.
At the top of my code page I have my shebang line, following by a HTML header statement.
I then do the CGI form validation part, and within this I have tried doing something like: print this.cgi( { 'Status' => '302 Moved', 'location' =>
'{http://localhost:10000/contents.html' } )
All that happens is that this line is printed at the top of the browser window, above my index.cgi page.
I hope this makes sense.
To redirect the browser to another URL you must output an 30X HTTP response that contains the Location: /foo/bar header. You can do that using the CGI#header method.
Instead of dealing with these details that you do not yet master, I suggest you use a simple framework as Sinatra or, at least, write your script as a Rack-compatible application.
If you really need to use the bare CGI class, have a look at this simple example: https://github.com/tdtds/amazon-auth-proxy/blob/master/amazon-auth-proxy.cgi.
Is it possible to return a json response in a Grails webflow ?
I'm wondering why you would? Webflows are designed to be "wizards" where you run from page to page, finally exiting the Flow and persisting the results, or not.
You do realize you can hit non-Flow actions from WebFlow pages? If you're writing an Ajax action for use in your Flow page, you can "render as JSON" in the action, and call the action directly using remote calls from the gsp.
There are many use cases in which integrating Ajax with Spring WebFlow would be very handy. Unfortunately, the Spring WebFlow is doing a poor job with that regards.
The work around here is to render a GSP and append it to the section of the page you are doing the Ajax call. Remember to pass "_eventId" when you build your Ajax URL.
I understand it is a valid case to expect JSON.
Work around:
Create a .gsp view with following content, and render it. (Of course, pass json string along by flow.json = (model as JSON).toString() !)
<%
response.contentType = 'application/json'
response.outputStream << json.getBytes()
%>
In an AJAX call back in drupal it is normally recommended to use drupal_json() to send data to client. This function converts the raw data into JSON along with HTML encoding.
I want to send the HTML data without encoding to client.
for this I am using following code:
print $html_output;
exit(0);
Is there any recommended or best way in drupal to do so?
If you need to output only the HTML output returned from the menu callback, then the following code is the correct one:
print $html_output;
module_invoke_all('exit');
exit();
If you want your output to appear together the blocks Drupal normally output, then the code needs to be changed to the following:
return $html_output;
That will do the trick. Allthough you should invoke hook_exit first. However this is shortcutting the framework some what, it may work in simple cases but wont work for forms etc.
The only time I have used this method is if I am printing some data which is allready json encoded.