Ruby on Rails 5 js.erb not being passed current instance variable











up vote
0
down vote

favorite












I have a comment model with like/unlike functionality and when I like any comment for the first time, it works correctly, because the like link was rendered with the initial html.erb rendering. But when I try to unlike or re-like anything, it always updates the partials with the link to the first comment I liked or unliked, respectively.



comments_controller.rb



def like
@comment = Comment.find(params[:id])
@comment.liked_by current_user
respond_to do |format|
format.html { redirect_to :back }
format.js { render '/comments/like.js.erb' }# layout: false }
end
end

def unlike
@comment = Comment.find(params[:id])
@comment.unliked_by current_user
respond_to do |format|
format.html { redirect_to :back }
format.js { render '/comments/unlike.js.erb' }# layout: false }
end
end


unlike.js.erb



$('.unlike_comment').bind('ajax:success', function(){

$(this).parent().parent().find('.vote_count').html('<%= escape_javascript @comment.votes_for.size.to_s %>');
$(this).closest('.unlike_comment').hide();

$(this).closest('.votes').html(' <%= link_to "", like_comment_path(@comment), remote: true, method: :get, class: 'like_comment glyphicon glyphicon-chevron-up', :style=>'color: #cecece;' %>');

});


like.js.erb



$('.like_comment').bind('ajax:success', function(){

$(this).parent().parent().find('.vote_count').html('<%= escape_javascript @comment.votes_for.size.to_s %>');
$(this).closest('.like_comment').hide();

$(this).closest('.votes').html(' <%= link_to "", unlike_comment_path(@comment), remote: true, method: :get, class: 'unlike_comment glyphicon glyphicon-chevron-up', :style=>'color: #202020;' %>');

});


And the relevant portion of _comment.html.erb



 <comment-body>
<p>
<span class="votes">
<% if logged_in? && (current_user.liked? comment) %>
<%= link_to "", unlike_comment_path(comment), method: :get, remote: true, class: 'unlike_comment glyphicon glyphicon-chevron-up', :style=>'color: #202020;' %>
<% elsif logged_in? %>
<%= link_to "", like_comment_path(comment), method: :get, remote: true, class: 'like_comment glyphicon glyphicon-chevron-up', :style=>'color: #cecece;' %>
<% else %>
<%= link_to "", prompt_login_comment_path(comment), method: :get, remote: true, class: 'prompt_login glyphicon glyphicon-chevron-up', :style=>'color: #cecece;' %>
<% end %>

</span>
<%= link_to comment.title, comment, class: 'bigger comment-text' %> <%= link_to comment.body, comment, class: 'notbig comment-text' %>
</p>
</comment-body>


So for instance, if I unlike comment 4, then like comment 3, it will change the unlike link in the comment 3 partial to comments/4/unlike. What might be going wrong that would cause this to occur? Any help is greatly appreciated.










share|improve this question


























    up vote
    0
    down vote

    favorite












    I have a comment model with like/unlike functionality and when I like any comment for the first time, it works correctly, because the like link was rendered with the initial html.erb rendering. But when I try to unlike or re-like anything, it always updates the partials with the link to the first comment I liked or unliked, respectively.



    comments_controller.rb



    def like
    @comment = Comment.find(params[:id])
    @comment.liked_by current_user
    respond_to do |format|
    format.html { redirect_to :back }
    format.js { render '/comments/like.js.erb' }# layout: false }
    end
    end

    def unlike
    @comment = Comment.find(params[:id])
    @comment.unliked_by current_user
    respond_to do |format|
    format.html { redirect_to :back }
    format.js { render '/comments/unlike.js.erb' }# layout: false }
    end
    end


    unlike.js.erb



    $('.unlike_comment').bind('ajax:success', function(){

    $(this).parent().parent().find('.vote_count').html('<%= escape_javascript @comment.votes_for.size.to_s %>');
    $(this).closest('.unlike_comment').hide();

    $(this).closest('.votes').html(' <%= link_to "", like_comment_path(@comment), remote: true, method: :get, class: 'like_comment glyphicon glyphicon-chevron-up', :style=>'color: #cecece;' %>');

    });


    like.js.erb



    $('.like_comment').bind('ajax:success', function(){

    $(this).parent().parent().find('.vote_count').html('<%= escape_javascript @comment.votes_for.size.to_s %>');
    $(this).closest('.like_comment').hide();

    $(this).closest('.votes').html(' <%= link_to "", unlike_comment_path(@comment), remote: true, method: :get, class: 'unlike_comment glyphicon glyphicon-chevron-up', :style=>'color: #202020;' %>');

    });


    And the relevant portion of _comment.html.erb



     <comment-body>
    <p>
    <span class="votes">
    <% if logged_in? && (current_user.liked? comment) %>
    <%= link_to "", unlike_comment_path(comment), method: :get, remote: true, class: 'unlike_comment glyphicon glyphicon-chevron-up', :style=>'color: #202020;' %>
    <% elsif logged_in? %>
    <%= link_to "", like_comment_path(comment), method: :get, remote: true, class: 'like_comment glyphicon glyphicon-chevron-up', :style=>'color: #cecece;' %>
    <% else %>
    <%= link_to "", prompt_login_comment_path(comment), method: :get, remote: true, class: 'prompt_login glyphicon glyphicon-chevron-up', :style=>'color: #cecece;' %>
    <% end %>

    </span>
    <%= link_to comment.title, comment, class: 'bigger comment-text' %> <%= link_to comment.body, comment, class: 'notbig comment-text' %>
    </p>
    </comment-body>


    So for instance, if I unlike comment 4, then like comment 3, it will change the unlike link in the comment 3 partial to comments/4/unlike. What might be going wrong that would cause this to occur? Any help is greatly appreciated.










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a comment model with like/unlike functionality and when I like any comment for the first time, it works correctly, because the like link was rendered with the initial html.erb rendering. But when I try to unlike or re-like anything, it always updates the partials with the link to the first comment I liked or unliked, respectively.



      comments_controller.rb



      def like
      @comment = Comment.find(params[:id])
      @comment.liked_by current_user
      respond_to do |format|
      format.html { redirect_to :back }
      format.js { render '/comments/like.js.erb' }# layout: false }
      end
      end

      def unlike
      @comment = Comment.find(params[:id])
      @comment.unliked_by current_user
      respond_to do |format|
      format.html { redirect_to :back }
      format.js { render '/comments/unlike.js.erb' }# layout: false }
      end
      end


      unlike.js.erb



      $('.unlike_comment').bind('ajax:success', function(){

      $(this).parent().parent().find('.vote_count').html('<%= escape_javascript @comment.votes_for.size.to_s %>');
      $(this).closest('.unlike_comment').hide();

      $(this).closest('.votes').html(' <%= link_to "", like_comment_path(@comment), remote: true, method: :get, class: 'like_comment glyphicon glyphicon-chevron-up', :style=>'color: #cecece;' %>');

      });


      like.js.erb



      $('.like_comment').bind('ajax:success', function(){

      $(this).parent().parent().find('.vote_count').html('<%= escape_javascript @comment.votes_for.size.to_s %>');
      $(this).closest('.like_comment').hide();

      $(this).closest('.votes').html(' <%= link_to "", unlike_comment_path(@comment), remote: true, method: :get, class: 'unlike_comment glyphicon glyphicon-chevron-up', :style=>'color: #202020;' %>');

      });


      And the relevant portion of _comment.html.erb



       <comment-body>
      <p>
      <span class="votes">
      <% if logged_in? && (current_user.liked? comment) %>
      <%= link_to "", unlike_comment_path(comment), method: :get, remote: true, class: 'unlike_comment glyphicon glyphicon-chevron-up', :style=>'color: #202020;' %>
      <% elsif logged_in? %>
      <%= link_to "", like_comment_path(comment), method: :get, remote: true, class: 'like_comment glyphicon glyphicon-chevron-up', :style=>'color: #cecece;' %>
      <% else %>
      <%= link_to "", prompt_login_comment_path(comment), method: :get, remote: true, class: 'prompt_login glyphicon glyphicon-chevron-up', :style=>'color: #cecece;' %>
      <% end %>

      </span>
      <%= link_to comment.title, comment, class: 'bigger comment-text' %> <%= link_to comment.body, comment, class: 'notbig comment-text' %>
      </p>
      </comment-body>


      So for instance, if I unlike comment 4, then like comment 3, it will change the unlike link in the comment 3 partial to comments/4/unlike. What might be going wrong that would cause this to occur? Any help is greatly appreciated.










      share|improve this question













      I have a comment model with like/unlike functionality and when I like any comment for the first time, it works correctly, because the like link was rendered with the initial html.erb rendering. But when I try to unlike or re-like anything, it always updates the partials with the link to the first comment I liked or unliked, respectively.



      comments_controller.rb



      def like
      @comment = Comment.find(params[:id])
      @comment.liked_by current_user
      respond_to do |format|
      format.html { redirect_to :back }
      format.js { render '/comments/like.js.erb' }# layout: false }
      end
      end

      def unlike
      @comment = Comment.find(params[:id])
      @comment.unliked_by current_user
      respond_to do |format|
      format.html { redirect_to :back }
      format.js { render '/comments/unlike.js.erb' }# layout: false }
      end
      end


      unlike.js.erb



      $('.unlike_comment').bind('ajax:success', function(){

      $(this).parent().parent().find('.vote_count').html('<%= escape_javascript @comment.votes_for.size.to_s %>');
      $(this).closest('.unlike_comment').hide();

      $(this).closest('.votes').html(' <%= link_to "", like_comment_path(@comment), remote: true, method: :get, class: 'like_comment glyphicon glyphicon-chevron-up', :style=>'color: #cecece;' %>');

      });


      like.js.erb



      $('.like_comment').bind('ajax:success', function(){

      $(this).parent().parent().find('.vote_count').html('<%= escape_javascript @comment.votes_for.size.to_s %>');
      $(this).closest('.like_comment').hide();

      $(this).closest('.votes').html(' <%= link_to "", unlike_comment_path(@comment), remote: true, method: :get, class: 'unlike_comment glyphicon glyphicon-chevron-up', :style=>'color: #202020;' %>');

      });


      And the relevant portion of _comment.html.erb



       <comment-body>
      <p>
      <span class="votes">
      <% if logged_in? && (current_user.liked? comment) %>
      <%= link_to "", unlike_comment_path(comment), method: :get, remote: true, class: 'unlike_comment glyphicon glyphicon-chevron-up', :style=>'color: #202020;' %>
      <% elsif logged_in? %>
      <%= link_to "", like_comment_path(comment), method: :get, remote: true, class: 'like_comment glyphicon glyphicon-chevron-up', :style=>'color: #cecece;' %>
      <% else %>
      <%= link_to "", prompt_login_comment_path(comment), method: :get, remote: true, class: 'prompt_login glyphicon glyphicon-chevron-up', :style=>'color: #cecece;' %>
      <% end %>

      </span>
      <%= link_to comment.title, comment, class: 'bigger comment-text' %> <%= link_to comment.body, comment, class: 'notbig comment-text' %>
      </p>
      </comment-body>


      So for instance, if I unlike comment 4, then like comment 3, it will change the unlike link in the comment 3 partial to comments/4/unlike. What might be going wrong that would cause this to occur? Any help is greatly appreciated.







      jquery ruby-on-rails ruby ruby-on-rails-5






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 18 at 22:40









      Kevin Van Dyke

      83




      83
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          your callback should target a fixed DOM class otherwise event handler will be overwritten every time ajax is successful(which is what you are experiencing). Also you only need to update anchor attributes, not redraw the whole anchor using html method.



          First make a unique class for each link eg comment_1



              <span class="votes">
          <% if logged_in? && (current_user.liked? comment) %>
          <%= link_to "", unlike_comment_path(comment), method: :get, remote: true, class: 'unlike_comment comment_#{comment.id} glyphicon glyphicon-chevron-up', :style=>'color: #202020;' %>
          <% elsif logged_in? %>
          <%= link_to "", like_comment_path(comment), method: :get, remote: true, class: "like_comment comment_#{comment.id} glyphicon glyphicon-chevron-up", :style=>'color: #cecece;' %>
          <% else %>
          <%= link_to "", prompt_login_comment_path(comment), method: :get, remote: true, class: 'prompt_login glyphicon glyphicon-chevron-up', :style=>'color: #cecece;' %>
          <% end %>
          </span>


          Then update unlike.js.erb template(doing the opposite for like.js.erb):



          $("." + "comment_#{@comment.id}").bind('ajax:success', function(){
          // Change class
          $(this).switchClass("like_comment", "unlike_comment");
          // Change path
          $(this).prop("src", "#{like_comment_path(comment)}");
          // anything else....
          });


          Let me know if it works.






          share|improve this answer





















          • That worked perfectly, thank you!
            – Kevin Van Dyke
            Nov 20 at 2:05











          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',
          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%2f53366153%2fruby-on-rails-5-js-erb-not-being-passed-current-instance-variable%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          your callback should target a fixed DOM class otherwise event handler will be overwritten every time ajax is successful(which is what you are experiencing). Also you only need to update anchor attributes, not redraw the whole anchor using html method.



          First make a unique class for each link eg comment_1



              <span class="votes">
          <% if logged_in? && (current_user.liked? comment) %>
          <%= link_to "", unlike_comment_path(comment), method: :get, remote: true, class: 'unlike_comment comment_#{comment.id} glyphicon glyphicon-chevron-up', :style=>'color: #202020;' %>
          <% elsif logged_in? %>
          <%= link_to "", like_comment_path(comment), method: :get, remote: true, class: "like_comment comment_#{comment.id} glyphicon glyphicon-chevron-up", :style=>'color: #cecece;' %>
          <% else %>
          <%= link_to "", prompt_login_comment_path(comment), method: :get, remote: true, class: 'prompt_login glyphicon glyphicon-chevron-up', :style=>'color: #cecece;' %>
          <% end %>
          </span>


          Then update unlike.js.erb template(doing the opposite for like.js.erb):



          $("." + "comment_#{@comment.id}").bind('ajax:success', function(){
          // Change class
          $(this).switchClass("like_comment", "unlike_comment");
          // Change path
          $(this).prop("src", "#{like_comment_path(comment)}");
          // anything else....
          });


          Let me know if it works.






          share|improve this answer





















          • That worked perfectly, thank you!
            – Kevin Van Dyke
            Nov 20 at 2:05















          up vote
          0
          down vote



          accepted










          your callback should target a fixed DOM class otherwise event handler will be overwritten every time ajax is successful(which is what you are experiencing). Also you only need to update anchor attributes, not redraw the whole anchor using html method.



          First make a unique class for each link eg comment_1



              <span class="votes">
          <% if logged_in? && (current_user.liked? comment) %>
          <%= link_to "", unlike_comment_path(comment), method: :get, remote: true, class: 'unlike_comment comment_#{comment.id} glyphicon glyphicon-chevron-up', :style=>'color: #202020;' %>
          <% elsif logged_in? %>
          <%= link_to "", like_comment_path(comment), method: :get, remote: true, class: "like_comment comment_#{comment.id} glyphicon glyphicon-chevron-up", :style=>'color: #cecece;' %>
          <% else %>
          <%= link_to "", prompt_login_comment_path(comment), method: :get, remote: true, class: 'prompt_login glyphicon glyphicon-chevron-up', :style=>'color: #cecece;' %>
          <% end %>
          </span>


          Then update unlike.js.erb template(doing the opposite for like.js.erb):



          $("." + "comment_#{@comment.id}").bind('ajax:success', function(){
          // Change class
          $(this).switchClass("like_comment", "unlike_comment");
          // Change path
          $(this).prop("src", "#{like_comment_path(comment)}");
          // anything else....
          });


          Let me know if it works.






          share|improve this answer





















          • That worked perfectly, thank you!
            – Kevin Van Dyke
            Nov 20 at 2:05













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          your callback should target a fixed DOM class otherwise event handler will be overwritten every time ajax is successful(which is what you are experiencing). Also you only need to update anchor attributes, not redraw the whole anchor using html method.



          First make a unique class for each link eg comment_1



              <span class="votes">
          <% if logged_in? && (current_user.liked? comment) %>
          <%= link_to "", unlike_comment_path(comment), method: :get, remote: true, class: 'unlike_comment comment_#{comment.id} glyphicon glyphicon-chevron-up', :style=>'color: #202020;' %>
          <% elsif logged_in? %>
          <%= link_to "", like_comment_path(comment), method: :get, remote: true, class: "like_comment comment_#{comment.id} glyphicon glyphicon-chevron-up", :style=>'color: #cecece;' %>
          <% else %>
          <%= link_to "", prompt_login_comment_path(comment), method: :get, remote: true, class: 'prompt_login glyphicon glyphicon-chevron-up', :style=>'color: #cecece;' %>
          <% end %>
          </span>


          Then update unlike.js.erb template(doing the opposite for like.js.erb):



          $("." + "comment_#{@comment.id}").bind('ajax:success', function(){
          // Change class
          $(this).switchClass("like_comment", "unlike_comment");
          // Change path
          $(this).prop("src", "#{like_comment_path(comment)}");
          // anything else....
          });


          Let me know if it works.






          share|improve this answer












          your callback should target a fixed DOM class otherwise event handler will be overwritten every time ajax is successful(which is what you are experiencing). Also you only need to update anchor attributes, not redraw the whole anchor using html method.



          First make a unique class for each link eg comment_1



              <span class="votes">
          <% if logged_in? && (current_user.liked? comment) %>
          <%= link_to "", unlike_comment_path(comment), method: :get, remote: true, class: 'unlike_comment comment_#{comment.id} glyphicon glyphicon-chevron-up', :style=>'color: #202020;' %>
          <% elsif logged_in? %>
          <%= link_to "", like_comment_path(comment), method: :get, remote: true, class: "like_comment comment_#{comment.id} glyphicon glyphicon-chevron-up", :style=>'color: #cecece;' %>
          <% else %>
          <%= link_to "", prompt_login_comment_path(comment), method: :get, remote: true, class: 'prompt_login glyphicon glyphicon-chevron-up', :style=>'color: #cecece;' %>
          <% end %>
          </span>


          Then update unlike.js.erb template(doing the opposite for like.js.erb):



          $("." + "comment_#{@comment.id}").bind('ajax:success', function(){
          // Change class
          $(this).switchClass("like_comment", "unlike_comment");
          // Change path
          $(this).prop("src", "#{like_comment_path(comment)}");
          // anything else....
          });


          Let me know if it works.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 at 1:02









          kasperite

          1,5561814




          1,5561814












          • That worked perfectly, thank you!
            – Kevin Van Dyke
            Nov 20 at 2:05


















          • That worked perfectly, thank you!
            – Kevin Van Dyke
            Nov 20 at 2:05
















          That worked perfectly, thank you!
          – Kevin Van Dyke
          Nov 20 at 2:05




          That worked perfectly, thank you!
          – Kevin Van Dyke
          Nov 20 at 2:05


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53366153%2fruby-on-rails-5-js-erb-not-being-passed-current-instance-variable%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

          Costa Masnaga

          Fotorealismo

          Sidney Franklin