js.erb not executing javascript but is processed rails
Add layout: false
option to the render block:
def pupil_leads
# some code here
respond_to do |f|
f.js { render layout: false, content_type: 'text/javascript' }
f.html
end
end
For some reason Rails don't recognize request as xhr
, I also watched that the views extension (.erb.html
or .slim
) must be specified in full.
js.erb rendered into layout, not executed
Perhaps you suffer from Rails bug/feature #10768?
I.e., such behavior occurs if your main layout is called application.haml
instead of application.html.haml
. it is because layouts without a format extension are rendered for all formats by default.
I can see you are using HAML, so am guessing this might be it.
Rails JQuery Ajax call succeeds, but js.erb data is not rendered unless I include unwanted characters
In the controller, when you do this:
respond_to do |format|
format.js
end
It means you are returning Javascript in the response, not HTML directly. Hence, when you wrap it in quotes or start with //
, it interprets it as Javascript string or comment line.
There are two ways to fix it.
The RJS Way
Remove following
success
block from your JQuery code:success: function(data){
$("#gl").html(data);
}Add Javascript in
get_gl_prefix_ajax.js.erb
file, which is what its meant for. It would look like this:$("#gl").html('<%= result %>');
The JSON Way
The other option is to return your data in JSON format from the controller, and then in
success
callback for$.ajax
, parse the JSON data and convert it into HTML before inserting in the DOM.
My Ajax call returns js.erb file as the response in the browser. How do I make it return json contents, but also execute the js.erb file?
How can I show json response in browser while js.erb file executes?
Thank you :)
If I understood correctly what you are asking, the answer is you can't. Your response's Content-Type
will be either an application/json
or an application/javascript
.
The browser will use this header to handle the response.
Related Topics
How to Get All Arguments of a Function as Single Object Inside That Function
How to Enable Touch on Multiple Buttons Simultaneously in React Native
Programming Language Independent Model Validation
Encrypting Data with Ruby Decrypting with Node
Browser-Independent Way to Detect When Image Has Been Loaded
Allow Only Numbers to Be Typed in a Textbox
How to Save HTML Page as PDF Using JavaScript or Jquery
Simplest Way to Wait Some Asynchronous Tasks Complete, in JavaScript
Embed a JavaScript Engine in an iOS Application
Converting JSON Format to CSV to Upload Data Table in R to Produce D3 Bubble Chart
Mixing Jsf El in a JavaScript File
Mediastream Capture Canvas and Audio Simultaneously
Wkwebview - Complex Communication Between JavaScript & Native Code
How to Serialize Dom Node to JSON Even If There Are Circular References
Resolving Require Paths with Webpack