Note that there are some explanatory texts on larger screens.

plurals
  1. POajax call to a different action doesn't work in rails 4
    text
    copied!<p>In my Rails 4 application I have a controller, items_controller.rb, which has an action "new":</p> <pre><code>def new respond_to do |format| format.html format.js end end </code></pre> <p>In the new.html.erb I have the following code which initiates an ajax call to the same action "new" when I click on "#aaa".</p> <pre><code>&lt;script&gt; $(document).ready(function() { $("#aaa").click(function() { $.ajax({ url: "/items/new", data: {}, async: true, dataType: 'script' }); }); }); &lt;/script&gt; </code></pre> <p>Here is new.js.erb:</p> <pre><code>$("#test_tag").html("qqq"); </code></pre> <p>This code works perfectly fine and when I click on "#aaa", it modifies the html of "#test_tag" to "qqq" as expected. Now if I just try to do the same using another action in my items_controller.rb say "htmlupdate" it won't work. All I do is change the url in the ajax call (in my new.html.erb):</p> <pre><code>&lt;script&gt; $(document).ready(function() { $("#aaa").click(function() { $.ajax({ url: "/items/htmlupdate", data: {}, async: true, dataType: 'script' }); }); }); &lt;/script&gt; </code></pre> <p>and create a method htmlupdate in my items_controller </p> <pre><code>def htmlupdate respond_to do |format| format.html format.js end end </code></pre> <p>and create a file htmlupdate.js.erb with the content:</p> <pre><code>$("#test_tag").html("qqq"); </code></pre> <p>Now, nothing works. Why is that? Is there something wrong with my url: ... syntax? I really appreciate your help.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload