Js.Erb Not Executing JavaScript But Is Processed Rails

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.

  1. 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 %>');
  2. 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



Leave a reply



Submit