My like/unlike button are not working properly when page is reloaded after liking a post












0














My code in index.html.erb is:



<li class="list-group-item">
Post --> <%= post.message %>
<% if logged_in? && @current_user != user %>
<% if find_like(post.id, @current_user.id) != 0 %>
<%= link_to "Unlike", 'javascript:void(0)', class: "btn btn-default like-right-position like-btn", data: { user_id: @current_user.id, likeable_id: post.id, likeable_type: 'Post', like: false } %>
<% else %>
<%= link_to "Like", 'javascript:void(0)', class: "btn btn-default like-right-position like-btn", data: { user_id: @current_user.id, likeable_id: post.id, likeable_type: 'Post', like: true } %>
<% end %>
<% end %>
<span id="post_<%= post.id %>_likes" class="like-right-position" data-like-id='0'><%= pluralize(post.likes.size, "like") %></span>
</li>


application_helper.rb file



def find_like post_id, user_id
like = Post.find(post_id).likes.find_by(user_id: user_id)
return like.present? ? like.id : 0
end


In the file below I have a problem with how to pass the attribute in the delete method ajax request
application.js file



$('.like-btn').click(function(){
var likeable_id = $(this).attr('data-likeable-id');
var likeable_type = $(this).attr('data-likeable-type');
var user_id = $(this).attr('data-user-id');
var like_id = $('#post_' + likeable_id + '_likes').attr('data-like-id');
if ($(this).html() == "Like") {
$.ajax({
type: "POST",
url: "/users/" + user_id + "/likes",
dataType: 'script',
data: {
'likeable_id': likeable_id,
'likeable_type': likeable_type,
'like': true
}
});
$(this).html('Unlike');
}
else {
$.ajax({
type: "DELETE",
url: "/users/" + user_id + "/likes/" + like_id,
dataType: 'script',
data: {
'likeable_id': likeable_id,
'like': false
}
});
$(this).html('Like');
}
return false;
});


likes_controller.rb



class LikesController < ApplicationController
def new
@like = Like.new
end
def create
@post = Post.find(params[:likeable_id])
@like = Like.create( like: params[:like], likeable_id: params[:likeable_id], likeable_type: params[:likeable_type], user_id: params[:user_id] )
if @like.present?
respond_to do |format|
format.html { redirect_back(fallback_location: root_path) }
format.js
end
else
redirect_back(fallback_location: root_path)
end
end
def destroy
@post = Post.find(params[:likeable_id])
@like = Like.find(params[:id])
respond_to do |format|
format.html { redirect_to(fallback_location: root_path) }
format.js {}
end
@like.destroy
end
end


create.js.erb



$('#post_<%= @post.id %>_likes').empty().append('<span id="post_<%= @post.id %>_likes" class="like-right-position"><%= pluralize(@post.likes.size, "like") %></span>');
$('#post_<%= @post.id %>_likes').attr('data-like-id', "<%= @like.id %>" );


delete.js.erb



$('#post_<%= @post.id %>_likes').attr('data-like-id', "0" );
$('#post_<%= @post.id %>_likes').empty().append('<span id="post_<%= @post.id %>_likes" class="like-right-position"><%= pluralize(@post.likes.size, "like") %></span>');


My like/unlike button on the page is working but after liking a post and then reloading the page, the unlike button is not working.
Thanks in advance!!!










share|improve this question



























    0














    My code in index.html.erb is:



    <li class="list-group-item">
    Post --> <%= post.message %>
    <% if logged_in? && @current_user != user %>
    <% if find_like(post.id, @current_user.id) != 0 %>
    <%= link_to "Unlike", 'javascript:void(0)', class: "btn btn-default like-right-position like-btn", data: { user_id: @current_user.id, likeable_id: post.id, likeable_type: 'Post', like: false } %>
    <% else %>
    <%= link_to "Like", 'javascript:void(0)', class: "btn btn-default like-right-position like-btn", data: { user_id: @current_user.id, likeable_id: post.id, likeable_type: 'Post', like: true } %>
    <% end %>
    <% end %>
    <span id="post_<%= post.id %>_likes" class="like-right-position" data-like-id='0'><%= pluralize(post.likes.size, "like") %></span>
    </li>


    application_helper.rb file



    def find_like post_id, user_id
    like = Post.find(post_id).likes.find_by(user_id: user_id)
    return like.present? ? like.id : 0
    end


    In the file below I have a problem with how to pass the attribute in the delete method ajax request
    application.js file



    $('.like-btn').click(function(){
    var likeable_id = $(this).attr('data-likeable-id');
    var likeable_type = $(this).attr('data-likeable-type');
    var user_id = $(this).attr('data-user-id');
    var like_id = $('#post_' + likeable_id + '_likes').attr('data-like-id');
    if ($(this).html() == "Like") {
    $.ajax({
    type: "POST",
    url: "/users/" + user_id + "/likes",
    dataType: 'script',
    data: {
    'likeable_id': likeable_id,
    'likeable_type': likeable_type,
    'like': true
    }
    });
    $(this).html('Unlike');
    }
    else {
    $.ajax({
    type: "DELETE",
    url: "/users/" + user_id + "/likes/" + like_id,
    dataType: 'script',
    data: {
    'likeable_id': likeable_id,
    'like': false
    }
    });
    $(this).html('Like');
    }
    return false;
    });


    likes_controller.rb



    class LikesController < ApplicationController
    def new
    @like = Like.new
    end
    def create
    @post = Post.find(params[:likeable_id])
    @like = Like.create( like: params[:like], likeable_id: params[:likeable_id], likeable_type: params[:likeable_type], user_id: params[:user_id] )
    if @like.present?
    respond_to do |format|
    format.html { redirect_back(fallback_location: root_path) }
    format.js
    end
    else
    redirect_back(fallback_location: root_path)
    end
    end
    def destroy
    @post = Post.find(params[:likeable_id])
    @like = Like.find(params[:id])
    respond_to do |format|
    format.html { redirect_to(fallback_location: root_path) }
    format.js {}
    end
    @like.destroy
    end
    end


    create.js.erb



    $('#post_<%= @post.id %>_likes').empty().append('<span id="post_<%= @post.id %>_likes" class="like-right-position"><%= pluralize(@post.likes.size, "like") %></span>');
    $('#post_<%= @post.id %>_likes').attr('data-like-id', "<%= @like.id %>" );


    delete.js.erb



    $('#post_<%= @post.id %>_likes').attr('data-like-id', "0" );
    $('#post_<%= @post.id %>_likes').empty().append('<span id="post_<%= @post.id %>_likes" class="like-right-position"><%= pluralize(@post.likes.size, "like") %></span>');


    My like/unlike button on the page is working but after liking a post and then reloading the page, the unlike button is not working.
    Thanks in advance!!!










    share|improve this question

























      0












      0








      0







      My code in index.html.erb is:



      <li class="list-group-item">
      Post --> <%= post.message %>
      <% if logged_in? && @current_user != user %>
      <% if find_like(post.id, @current_user.id) != 0 %>
      <%= link_to "Unlike", 'javascript:void(0)', class: "btn btn-default like-right-position like-btn", data: { user_id: @current_user.id, likeable_id: post.id, likeable_type: 'Post', like: false } %>
      <% else %>
      <%= link_to "Like", 'javascript:void(0)', class: "btn btn-default like-right-position like-btn", data: { user_id: @current_user.id, likeable_id: post.id, likeable_type: 'Post', like: true } %>
      <% end %>
      <% end %>
      <span id="post_<%= post.id %>_likes" class="like-right-position" data-like-id='0'><%= pluralize(post.likes.size, "like") %></span>
      </li>


      application_helper.rb file



      def find_like post_id, user_id
      like = Post.find(post_id).likes.find_by(user_id: user_id)
      return like.present? ? like.id : 0
      end


      In the file below I have a problem with how to pass the attribute in the delete method ajax request
      application.js file



      $('.like-btn').click(function(){
      var likeable_id = $(this).attr('data-likeable-id');
      var likeable_type = $(this).attr('data-likeable-type');
      var user_id = $(this).attr('data-user-id');
      var like_id = $('#post_' + likeable_id + '_likes').attr('data-like-id');
      if ($(this).html() == "Like") {
      $.ajax({
      type: "POST",
      url: "/users/" + user_id + "/likes",
      dataType: 'script',
      data: {
      'likeable_id': likeable_id,
      'likeable_type': likeable_type,
      'like': true
      }
      });
      $(this).html('Unlike');
      }
      else {
      $.ajax({
      type: "DELETE",
      url: "/users/" + user_id + "/likes/" + like_id,
      dataType: 'script',
      data: {
      'likeable_id': likeable_id,
      'like': false
      }
      });
      $(this).html('Like');
      }
      return false;
      });


      likes_controller.rb



      class LikesController < ApplicationController
      def new
      @like = Like.new
      end
      def create
      @post = Post.find(params[:likeable_id])
      @like = Like.create( like: params[:like], likeable_id: params[:likeable_id], likeable_type: params[:likeable_type], user_id: params[:user_id] )
      if @like.present?
      respond_to do |format|
      format.html { redirect_back(fallback_location: root_path) }
      format.js
      end
      else
      redirect_back(fallback_location: root_path)
      end
      end
      def destroy
      @post = Post.find(params[:likeable_id])
      @like = Like.find(params[:id])
      respond_to do |format|
      format.html { redirect_to(fallback_location: root_path) }
      format.js {}
      end
      @like.destroy
      end
      end


      create.js.erb



      $('#post_<%= @post.id %>_likes').empty().append('<span id="post_<%= @post.id %>_likes" class="like-right-position"><%= pluralize(@post.likes.size, "like") %></span>');
      $('#post_<%= @post.id %>_likes').attr('data-like-id', "<%= @like.id %>" );


      delete.js.erb



      $('#post_<%= @post.id %>_likes').attr('data-like-id', "0" );
      $('#post_<%= @post.id %>_likes').empty().append('<span id="post_<%= @post.id %>_likes" class="like-right-position"><%= pluralize(@post.likes.size, "like") %></span>');


      My like/unlike button on the page is working but after liking a post and then reloading the page, the unlike button is not working.
      Thanks in advance!!!










      share|improve this question













      My code in index.html.erb is:



      <li class="list-group-item">
      Post --> <%= post.message %>
      <% if logged_in? && @current_user != user %>
      <% if find_like(post.id, @current_user.id) != 0 %>
      <%= link_to "Unlike", 'javascript:void(0)', class: "btn btn-default like-right-position like-btn", data: { user_id: @current_user.id, likeable_id: post.id, likeable_type: 'Post', like: false } %>
      <% else %>
      <%= link_to "Like", 'javascript:void(0)', class: "btn btn-default like-right-position like-btn", data: { user_id: @current_user.id, likeable_id: post.id, likeable_type: 'Post', like: true } %>
      <% end %>
      <% end %>
      <span id="post_<%= post.id %>_likes" class="like-right-position" data-like-id='0'><%= pluralize(post.likes.size, "like") %></span>
      </li>


      application_helper.rb file



      def find_like post_id, user_id
      like = Post.find(post_id).likes.find_by(user_id: user_id)
      return like.present? ? like.id : 0
      end


      In the file below I have a problem with how to pass the attribute in the delete method ajax request
      application.js file



      $('.like-btn').click(function(){
      var likeable_id = $(this).attr('data-likeable-id');
      var likeable_type = $(this).attr('data-likeable-type');
      var user_id = $(this).attr('data-user-id');
      var like_id = $('#post_' + likeable_id + '_likes').attr('data-like-id');
      if ($(this).html() == "Like") {
      $.ajax({
      type: "POST",
      url: "/users/" + user_id + "/likes",
      dataType: 'script',
      data: {
      'likeable_id': likeable_id,
      'likeable_type': likeable_type,
      'like': true
      }
      });
      $(this).html('Unlike');
      }
      else {
      $.ajax({
      type: "DELETE",
      url: "/users/" + user_id + "/likes/" + like_id,
      dataType: 'script',
      data: {
      'likeable_id': likeable_id,
      'like': false
      }
      });
      $(this).html('Like');
      }
      return false;
      });


      likes_controller.rb



      class LikesController < ApplicationController
      def new
      @like = Like.new
      end
      def create
      @post = Post.find(params[:likeable_id])
      @like = Like.create( like: params[:like], likeable_id: params[:likeable_id], likeable_type: params[:likeable_type], user_id: params[:user_id] )
      if @like.present?
      respond_to do |format|
      format.html { redirect_back(fallback_location: root_path) }
      format.js
      end
      else
      redirect_back(fallback_location: root_path)
      end
      end
      def destroy
      @post = Post.find(params[:likeable_id])
      @like = Like.find(params[:id])
      respond_to do |format|
      format.html { redirect_to(fallback_location: root_path) }
      format.js {}
      end
      @like.destroy
      end
      end


      create.js.erb



      $('#post_<%= @post.id %>_likes').empty().append('<span id="post_<%= @post.id %>_likes" class="like-right-position"><%= pluralize(@post.likes.size, "like") %></span>');
      $('#post_<%= @post.id %>_likes').attr('data-like-id', "<%= @like.id %>" );


      delete.js.erb



      $('#post_<%= @post.id %>_likes').attr('data-like-id', "0" );
      $('#post_<%= @post.id %>_likes').empty().append('<span id="post_<%= @post.id %>_likes" class="like-right-position"><%= pluralize(@post.likes.size, "like") %></span>');


      My like/unlike button on the page is working but after liking a post and then reloading the page, the unlike button is not working.
      Thanks in advance!!!







      javascript jquery ruby-on-rails ajax






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 at 8:52









      Pujan Soni

      189




      189





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389292%2fmy-like-unlike-button-are-not-working-properly-when-page-is-reloaded-after-likin%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389292%2fmy-like-unlike-button-are-not-working-properly-when-page-is-reloaded-after-likin%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Create new schema in PostgreSQL using DBeaver

          Deepest pit of an array with Javascript: test on Codility

          Costa Masnaga