【问题标题】:Like/Dislike in rails via ajax通过ajax喜欢/不喜欢rails
【发布时间】:2015-12-18 20:02:53
【问题描述】:

我想在我的 RoR 应用程序上添加喜欢/不喜欢的能力。我怎样才能通过 Ajax 请求实现它?

不喜欢和喜欢 - 是整数我怎样才能发出 Ajax 请求,而不是我可以发送我想要在我的方法中增加“喜欢”或“不喜欢”计数器的数据

我有一张有帖子的桌子:

#app/views/dashboard/view.html.erb
<table>
  <%if @post.count!=0%>
    <%@post.each do |p|%>
      <%if !p.text.nil?%>
      <tr>
       <td><b class="margin"><h4><%=p.text%></b></h4></td>
       <td>by <%=p.user.username%>&nbsp;&nbsp; </td>
       <td><span class="glyphicon glyphicon-thumbs-up likeAction"><%= link_to p.like, dashboard_like_path, :remote => true, :id => 'likecount' %> </td>
       <td><span class="glyphicon glyphicon-thumbs-down"><%= link_to p.dislike, dashboard_dislike_path, :remote => true, :id => 'dislikecount' %> </td>
      <%end%>
    <% end %>
  <%else%>
    There's no posts yet, but you can add <%=link_to "one", dashboard_posts_create_a_post_path%>
  <%end%>
</table>

我的 js 文件

#app/views/dashboard/view.js
$('#likecount').text(@post.like);
$('#dislikecount').text(@post.dislike);

我在控制器中的方法:

      #app/controller/dahsboard_controller.rb  
      def like
        @post.increment!(:like)
        respond_to do |format|
          format.html
          format.js
        end
      end

      def dislike
        @post.increment!(:dislike)
        respond_to do |format|
          format.html
          format.js
        end
      end

我在 assets/javascripts 中的dashboard.js

jQuery(function($) {
  $("likeAction").click(function(){
     $.ajax({
        url: dashboard_like_path,
        type: 'POST',
        success: function(){
            $('#linkcount').text(data);
        }
        error: function(error){
           alert(error);
        }
     });
  });
});

【问题讨论】:

    标签: javascript jquery ruby-on-rails ajax ruby-on-rails-4


    【解决方案1】:

    您已经拥有 Rails 内置的 AJAX 功能,因此无需调用 $.ajax。只需在您的link_to 'Like', ..., remote: true 上设置remote: true 并使用您在app/views/dashboard/view.js 中的相同代码进行响应:format.js { render action: 'view' }

    编辑:只要将喜欢和不喜欢设置为帖子上的成员路由:

                                  dislike_post POST   /posts/:id/dislike(.:format)                                             posts#dislike
                                     like_post POST   /posts/:id/like(.:format)                                                posts#like
    

    如果您与 show、like 和 dislike 共享此代码,您将有一个 params[:id](如果您发送一个)来执行@post = Post.find(params[:id]) 之类的操作。你可以在过滤器之前创建一个set_post,所以你不要重复自己。

    【讨论】:

    • 我得到一个错误,我在我的方法中得到一个 nil 文件?如何将我的 post 变量发送到此方法中?
    • @post 为零?只需从参数 [:id] 中获取 if,就像你在节目中所做的那样:@post = Post.find(params[:id]) 喜欢和不喜欢应该是成员路线,所以他们有 :id
    • 如果 params[:id] 包含当前登录用户的 id,我如何从@post 获取 id?
    • params[:id] 有你通过的任何东西,dislike_post_path(post) 会将 post.id 发送为params[:id]。让我们看看你的这两个动作的路线,我无法从&lt;%= link_to p.like, dashboard_like_path, :remote =&gt; true, :id =&gt; 'likecount' %&gt; 推断它们
    【解决方案2】:

    您可能想查看一个名为 acts_as_votable 的宝石

    这设置了您的大部分模型功能 - 允许您使用 @post.downvote_from @user2 等之类的东西。我会让您研究一下,因为我认为这是您在后端所需要的。


    关于前端(尤其是 Ajax),你必须设置一个控制器动作,然后用一个 JS 请求来点击它:

    #config/routes.rb
    resources :posts do
       match :vote, via: [:post,:delete]
    end
    
    #app/controllers/posts_controller.rb
    class PostsController < ApplicationController
       respond_to :js, :html, only: :vote
    
       def vote
          if request.delete?
             #downvote
          elsif request.post?
             #upvote
          end
       end
    end
    

    这将允许您使用以下内容:

    #app/views/posts/vote.js.erb
    $(".element").html("<%=j render partial: "post/vote_count", object: @post %>");
    
    #app/views/posts/index.html.erb
    <% @posts.each do |post| %>
       <%= render partial: "post/vote_count", object: :post %>
    <% end %>
    
    #app/views/posts/_vote_count.html.erb
    <% method = @post.liked_by(current_user)
    <%= link_to post.likes, post_vote_path(post), method: :post, remote: true %>
    

    --

    Ajax 功能预置在 Rails 中;您必须小心它会将您发送到哪个控制器操作,以及给出的响应。

    我上面的代码使用respond_to 块来调用.js.erb 响应——允许您在发送请求时执行一些操作。

    【讨论】:

    • 谢谢,感谢您的建议,但是如何使用我的代码解决我的问题(只是更正它)?
    猜你喜欢
    • 2012-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    • 2016-12-31
    • 2014-09-21
    • 2012-09-09
    • 2015-05-13
    相关资源
    最近更新 更多