3

Im struggling to make my Like links to work with ajax. After clicking on "Like" nothing changes, only after refreshing the page I can see that like got counted.

EDIT: Addet routes. EDIT2: I inspected the Like link with Firebug and after entering the code from like.js.erb file it shows message: Image My like.js.erb file:

        <% if Like.find_by(likeable: @post) %>
          $(".likes").html("<%= escape_javascript(render 'posts/unlike' ) %>");
        <% else %>
          $(".likes").html("<%= escape_javascript(render 'posts/like' ) %>");
        <% end %>

(I have checked and If conditions works fine). My partial for posts (_post.html.erb).

    <li class="post">
        <span class="user"><%= link_to post.user.name, post.user %></span>
        <br>
        <span class="title"><%= post.title %></span>
        <br>
        <span class="ava"><%= image_tag post.avatar(:large) %></span>
        <br>
        <span class="timestamp">
            Posted <%= time_ago_in_words(post.created_at) %> ago.
            <% if logged_in? && current_user.admin? %>
                <%= link_to "delete", post, method: :delete,
                                                                        data: { confirm: "You sure?" } %>
            <% end %>
################## START OF THE LIKES #####################
            <span class="likes">
                <span class="like">
                <% if logged_in? %>
                    <% if !current_user.likes?(current_user, post) %>
                        <%= render 'posts/like', post: post %>
                        <!-- <%= link_to "Like", like_post_path(post, like: true), method: 'post', remote: true %> -->
                    </span>
                    <% else %>
                    <span class="unlike">
                        <%= render 'posts/unlike', post: post %>
                        <!-- <%= link_to "Unlike", like_post_path(post, like: false), method: 'post', remote: true %>-->
                    </span>
                    <% end %>
                    (<%= pluralize( post.likes.size, "like") %>)
                <% else %>
                    <%= link_to "Like", login_path,
                          data: { confirm: "You need to log in to like" } %>
                    (<%= pluralize( post.likes.size, "like") %>)
                <% end %>
            </span>
################## THE END OF THE LIKES #####################
        </span>
    </li>

And the like method in the post controller.

def like
    post = Post.find(params[:id])
    if Like.find_by(likeable: post)
        Like.find_by(likeable: post).destroy
        respond_to do |format|
            format.html do
                flash[:success] = "Like Updated!"
                redirect_to :back
            end
            format.js
            end
    else
        Like.create(likeable: post, user: current_user, like: params[:like])
        respond_to do |format|
            format.html do
                flash[:success] = "Like Updated!"
                redirect_to :back
            end
            format.js
        end
    end
end

And Like and Unlike partials I use: Like:

<%= link_to "Like", like_post_path(post, like: true), method: 'post', remote: true %>

Unlike:

<%= link_to "Like", like_post_path(post, like: false), method: 'post', remote: true %>

Any help or advice would be nice. I know that my code is rather in bad condition so don't be afraid to suggest some changes in it. And finally Please help me, I have wasted tons of time on trying to make it work but no results yet.

Routes:

Rails.application.routes.draw do
    root 'main_page#home'
    get         'top_list'  => 'main_page#top_list'
    get         'sign_up'   => 'users#new'
    get         'posts/new'
    get         'login'         => 'sessions#new'
    post        'login'         => 'sessions#create'
    delete  'logout'        => 'sessions#destroy'
    resources :users
    resources :posts do
        member do
            post 'like'
        end
    end
end

There are logs after I click on Like.

Completed 500 Internal Server Error in 66ms (ActiveRecord: 41.0ms)

NameError (undefined local variable or method `post' for #<#<Class:0x007f8ce976ff48>:0x007f8ce976f3e0>):
  app/views/posts/_unlike.html.erb:1:in `_app_views_posts__unlike_html_erb___187529012528174235_70121703666860'
  app/views/posts/like.js.erb:4:in `_app_views_posts_like_js_erb__3375438513772599537_70121703368540'
  app/controllers/posts_controller.rb:38:in `like'
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Jakub Kopyś
  • 670
  • 8
  • 20
  • Would you add your routes too? It's weird that that like.js.erb is not compiling the erb. Try to put a `<% raise %>` in that template and see if you can get it to raise. Also, if you haven't tried - better_errors gem is a great way to help debug – Mark Swardstrom Oct 15 '15 at 19:52
  • One note - I checked and after changing $(".likes").html("<%= escape_javascript(render 'posts/unlike' ) %>"); to $(".likes").html("foobar"); It actually normally changes to foobar so I am not sure if this error is the source of the problem. And yes - adding <% raise %> to like and unlike partials both raise an error. On the other hand adding it to js.erb file does not raise - but I am not sure if It should. – Jakub Kopyś Oct 15 '15 at 20:02
  • Additionally better_errors gem shows nothing - it does not raise any errors. – Jakub Kopyś Oct 15 '15 at 21:08
  • You should see an error (at least in the logs) with raise in the js.erb file. – Mark Swardstrom Oct 15 '15 at 21:11
  • Well I fell bad now for not thinking of that. I edited the question above and added logs. Looks like the partial does not see the variable, but I can't see why - I pass it <%= render 'posts/like', post: post %>. Any suggestions? – Jakub Kopyś Oct 15 '15 at 21:21
  • `post = Post.find(params[:id])` should be `@post = Post.find(params[:id])` ? You'll need that to be an @ variable to get to the partial. – Mark Swardstrom Oct 15 '15 at 21:29
  • Thank You, That was the problem - could I give you credit for Your help in any way? Because I can;t upvote these comments sadly. – Jakub Kopyś Oct 15 '15 at 21:32
  • That's OK. But, was it a problem with html_safe? I've never needed to use that. – Mark Swardstrom Oct 15 '15 at 21:35
  • Not - this was fault of lack of post: @post in js.erb render file and not setting post to instance variable with @. – Jakub Kopyś Oct 15 '15 at 21:38

1 Answers1

1

Try adding html_safe at the end. Also, I think render needs post as well (like you do in views)

$(".likes").html('<%= escape_javascript(render "posts/unlike",post: @post).html_safe %>');

http://apidock.com/rails/String/html_safe

basiam
  • 1,567
  • 19
  • 25