Note that there are some explanatory texts on larger screens.

plurals
  1. POAjax Request using Rails and jQuery Problem
    primarykey
    data
    text
    <p>I have a favourite and un-favourite functionality in my application and I am using jQuery. This functionality works partially. The page gets loaded, and when I click the 'favourite' button(it is inside add_favourite_div element), it sends a XHR request and the post is set as favourite. Then a new div called "remove_favourite_div" replaces its place.Now when I click the remove favourite(which is part of remove_favourite_div), it sends a normal http request inside of xhr.</p> <p>The structure when the page gets loaded first time</p> <pre><code>&lt;div id="favourite"&gt; &lt;div id="add_favourite_div"&gt; &lt;form method="post" id="add_favourite" action="/viewpost/add_favourite"&gt; &lt;div style="margin: 0pt; padding: 0pt; display: inline;"&gt; &lt;input type="hidden" value="w873BgYHLxQmadUalzMRUC+1ql4AtP3U7f78dT8x9ho=" name="authenticity_token"&gt; &lt;/div&gt; &lt;input type="hidden" value="3" name="Favourite[post_id]" id="Favourite_place_id"&gt; &lt;input type="hidden" value="2" name="Favourite[user_id]" id="Favourite_user_id"&gt; &lt;input type="submit" value="Favourite" name="commit"&gt;&lt;br&gt; &lt;/form&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>DOM after clicking on the unfavourite button</p> <pre><code>&lt;div id="favourite"&gt; &lt;div id="remove_favourite_div"&gt; &lt;form method="post" id="remove_favourite" action="/viewpost/remove_favourite"&gt; &lt;div style="margin: 0pt; padding: 0pt; display: inline;"&gt; &lt;input type="hidden" value="w873BgYHLxQmadUalzMRUC+1ql4AtP3U7f78dT8x9ho=" name="authenticity_token"&gt; &lt;/div&gt; &lt;input type="hidden" value="3" name="Favourite[post_id]" id="Favourite_place_id"&gt; &lt;input type="hidden" value="2" name="Favourite[user_id]" id="Favourite_user_id"&gt; &lt;input type="submit" value="UnFavourite" name="commit"&gt;&lt;br&gt; &lt;/form&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>In my application.js, I have two functions to trigger the xhr request</p> <pre><code>$("#add_favourite").submit(function(){ alert("add favourite"); action = $(this).attr("action") $.post(action,$(this).serialize(),null,"script"); return false; }); $("#remove_favourite").submit(function(){ alert("remove favourite"); action = $(this).attr("action"); $.post(action,$(this).serialize(),null,"script"); return false; }); </code></pre> <p>Here, when the post is initially not a favourite, favourite button is displayed and when i clicked on the button, $("#add_favourite").submit gets called and unfavourite form is displayed correctly, but now when I click on the un-favourite button, $("#remove_favourite").submit does not get called.</p> <p>The whole scenario is true in both ways, I mean favourite->Unfavourite and Unfavourite->favourite</p> <p>Can someone please help me to solve this Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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