Rails 4 Ajax类按钮无法工作



我努力使我的喜欢链接与ajax工作。点击"点赞"后没有任何变化,只有在刷新页面后我才能看到点赞被计数。

编辑:添加路由我用Firebug检查了Like链接,在从Like .js.erb文件输入代码后,它显示了消息:图像我的like.js.erb文件:

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

(我已经检查过了,如果条件正常)。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>

和post控制器中的like方法

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

我使用Like和Like的偏导数:如:

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

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

任何帮助或建议都会很好。我知道我的代码状况相当糟糕,所以请不要害怕提出一些修改建议。最后,请帮助我,我已经浪费了大量的时间,试图使它工作,但没有结果。

路线:

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

点击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'

尝试在末尾添加html_safe。此外,我认为渲染也需要post(就像你在视图中所做的那样)

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

相关内容

  • 没有找到相关文章

最新更新