Thumbs_up宝石轨 - 如何拿回来一票呢?一票、Thumbs_up

2023-09-10 18:30:48 作者:成熟小男人

我终于得到了我的票被通过AJAX提交正确的,不过,我可以'似乎unvote的。这是我的模型:

I finally got my votes being submitted correctly via ajax, however I can' seem to unvote at all. Here's my models:

class User < ActiveRecord::Base

    acts_as_voter
end

class Vendor < ActiveRecord::Base

    acts_as_voteable
end

我的路线:

resources :vendors do
    collection do
      post :vote_for_vendor
      delete :vote_against_vendor
    end
  end

我的控制器:

My controller:

class VendorsController < ApplicationController

    def vote_for_vendor
        begin
            vendor = Vendor.find(params[:vendor_id])
            current_user.vote_for(vendor)
            render :nothing => true, :status => 200
        rescue ActiveRecord::RecordInvalid
            render :nothing => true, :status => 404
        end
    end

    def vote_against_vendor
        begin
            vendor = Vendor.find(params[:vendor_id])
            current_user.vote_against(vendor)
            render :nothing => true, :status => 404
        rescue ActiveRecord::RecordInvalid
            render :nothing => true, :status => 404
        end
    end
end

我的看法:

<table class="table table-condensed table-hover">
    <tr>
        <th>Name</th>
        <th>Address</th>
        <th>Favorite?</th>
    </tr>
        <% @vendors.each do |v| %>
    <tr>
        <td><%= v.name %></td>
        <td><%= v.address %></td>
        <td id="toggle">
            <% if current_user.voted_for?(v) %>
                <%= button_to "Unlike", { :controller => :vendors, :action => 'vote_against_vendor', :vendor_id => v.id},
                                        { :method => 'delete', :remote => true }%>
            <% else %>
                <%= button_to "Like", { :controller => :vendors, :action => 'vote_for_vendor', :vendor_id => v.id},
                                    { :method => 'create', :remote => true} %>
            <% end %>
        </td>
    </tr>
        <% end %>
</table>

所以,我怎么能unvote的供应商?我读了thumbs_up文档,默认情况下选民只能投票一次。这是否意味着,用户谁votes_for不能再vote_against?我实施的目的是为了有一个最喜欢的还是喜欢按钮。

So how can I unvote for a vendor? I read in the thumbs_up docs that by default a voter can only vote once. Does this mean that a user who votes_for can't then vote_against? The purpose of my implementation is to have a favorite or like button.

下面在单击就是我从服务器获取不同于:

Here's what I'm getting from the server upon clicking 'Unlike':

Started DELETE "/vendors/vote_against_vendor?vendor_id=1" for 127.0.0.1 at 2013-09-06 14:34:02 -0700
Processing by VendorsController#vote_against_vendor as JS
  Parameters: {"authenticity_token"=>"/ZdePkPe+4rM8thUa+81hEL68cw1CJn93P0LQqEMC3s=", "vendor_id"=>"1"}
  Vendor Load (0.2ms)  SELECT "vendors".* FROM "vendors" WHERE "vendors"."id" = ? LIMIT 1  [["id", "1"]]
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
   (0.1ms)  begin transaction
  Vote Exists (0.2ms)  SELECT 1 AS one FROM "votes" WHERE ("votes"."voteable_id" = 1 AND "votes"."voteable_type" = 'Vendor' AND "votes"."voter_type" = 'User' AND "votes"."voter_id" = 1) LIMIT 1
   (0.1ms)  rollback transaction
  Rendered text template (0.0ms)
Completed 404 Not Found in 7ms (Views: 0.6ms | ActiveRecord: 0.7ms | Solr: 0.0ms)

专家提示:通读文件很透,你看傻了之前,问一个愚蠢的quesiton。只需使用 unvote_for 的方法来完成这件事。不知道我怎么错过了它的第一次。

Pro Tip: Read through the documentation very thoroughly before you look silly and ask a dumb quesiton. Just use the unvote_for method to get this done. Not sure how I missed it the first time.

推荐答案

vote_against_vendor ,您直接拨打电话后, vote_against ,你这样做:

In vote_against_vendor, directly after you call vote_against, you do this:

render :nothing => true, :status => 404

当你也许应该这样做:

render :nothing => true, :status => 200
相关推荐