Note that there are some explanatory texts on larger screens.

plurals
  1. POLike button does not update with AJAX
    text
    copied!<p>I am passing a collection (@feed_items) to a _feed_item partial via the :collection option and converting it to dailypost with :as => :dailypost. </p> <p>Inside the _feed_item partial I rendered another partial for _like_button, and i used :locals to continue using dailypost.</p> <p>Everything works fine with the database. Likes get added and taken out :) BUT</p> <p><strong>I am trying to use (AJAX) to create.js.erb &amp; destroy.js.erb the like button. For some reason only the top post gets updated correctly and i have to refresh the page to see the ones below it.</strong></p> <p>I know the solution involves assigning a unique post id to each "like" element, say "like-123", but that is where i am stuck.......</p> <p>I also know that the problem may be in _feed_items.html.erb because i am passing two ids....any suggestions??</p> <p><strong>Views</strong></p> <p>_feed.html.erb</p> <pre><code>&lt;% if @feed_items.any? %&gt; &lt;ol&gt; &lt;%= render partial: 'shared/feed_item', collection: @feed_items, :as =&gt; :dailypost %&gt; &lt;/ol&gt; &lt;% end %&gt; </code></pre> <p>_feed_items.html.erb</p> <pre><code>&lt;li id="&lt;%= @dailypost.id %&gt;"&gt; &lt;span class="user"&gt; &lt;%= link_to dailypost.user.name, dailypost.user %&gt; &lt;span class="content"&gt;&lt;%= dailypost.content_html %&gt;&lt;/span&gt; &lt;div id="like"&gt; &lt;%= render :partial =&gt; 'likes/like_button', :locals =&gt;{:dailypost =&gt; dailypost} %&gt; &lt;/div&gt; &lt;/li&gt; </code></pre> <p>_like_button.html.erb</p> <pre><code>&lt;% if like = current_user.likes.find_by_dailypost_id(dailypost.id) %&gt; &lt;%= form_for like, :html =&gt; { :method =&gt; :delete }, :remote =&gt; true do |f| %&gt; &lt;%= f.submit "Unlike" %&gt; &lt;% end %&gt; &lt;% else %&gt; &lt;%= form_for current_user.likes.build, :remote =&gt; true do |f| %&gt; &lt;div&gt;&lt;%= f.hidden_field :dailypost_id, value: dailypost.id %&gt;&lt;/div&gt; &lt;%= f.hidden_field :user_id %&gt; &lt;%= f.submit "Like" %&gt; &lt;% end %&gt; &lt;% end %&gt; </code></pre> <p>create.js.erb</p> <pre><code>$("#like").html("&lt;%= escape_javascript(render :partial =&gt; 'like_button', :locals =&gt; {:dailypost =&gt; @dailypost}) %&gt;"); </code></pre> <p>destroy.js.erb</p> <pre><code>$("#like").html("&lt;%= escape_javascript(render :partial =&gt; 'like_button', :locals =&gt; {:dailypost =&gt; @dailypost}) %&gt;"); </code></pre> <p><strong>Controller</strong></p> <pre><code>class LikesController &lt; ApplicationController respond_to :html, :js def create @like = Like.create(params[:like]) @dailypost = @like.dailypost respond_to do |format| format.js format.html { redirect_to :back } end end def destroy like = Like.find(params[:id]).destroy @dailypost = like.dailypost respond_to do |format| format.js format.html { redirect_to :back } end end end </code></pre>
 

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