2016-08-05 63 views
0

在我的应用程序中会有很多东西users可以向上或向下投票(以类似Reddit的方式)。这将包括jokes,recipes,rules等的upvote/downvote结构。现在我尝试使用joke模型作为示例来设置多态性votesRails Polymorphic投票结构

我已经加入votes作为一个多态变量,可以在我的schema可以看出:

create_table "votes", force: :cascade do |t| 
    t.integer "value" 
    t.integer "user_id" 
    t.integer "voteable_id" 
    t.string "voteable_type" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    end 

    add_index "votes", ["user_id"], name: "index_votes_on_user_id" 
    add_index "votes", ["voteable_type", "voteable_id"], name: "index_votes_on_voteable_type_and_voteable_id" 

而且我加了float列到我的jokesrank

我创建了一个vote模型:

class Vote < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :voteable, polymorphic: true 
    after_save :update_vote 

    private 
    def update_joke 
    vote.update_rank 
    end 
end 

并做出了相应的更新,我joke型号:

class Joke < ActiveRecord::Base 
    belongs_to :user 
    has_many :votes, as: :voteable, dependent: :destroy 
    default_scope { order('rank DESC')} 
    def up_votes 
    votes.where(value: 1).count 
    end 
    def down_votes 
    votes.where(value: -1).count 
    end 
    def points 
    votes.sum(:value) 
    end 
    def update_rank 
    new_rank = points 
    update_attribute(:rank, new_rank) 
    end 
end 

而且我user型号:

class User < ActiveRecord::Base 
    ... 
    has_many :jokes, dependent: :destroy 
    has_many :votes, dependent: :destroy 
end 

我有一个votes_controller如下:

class VotesController < ApplicationController 

    def up_vote 
    update_vote(1) 
    redirect_to :back 
    end 

    def down_vote 
    update_vote(-1) 
    redirect_to :back 
    end 

    private 
    def update_vote(new_value) 
    @joke = Joke.find(params[:joke_id]) 
    @vote = @joke.votes.where(user_id: current_user.id).first 

    if @vote 
     @vote.update_attribute(:value, new_value) 
    else 
     @vote = current_user.votes.create(value: new_value, joke: @joke) 
    end 
    end 
end 

最后这个_voter.html.erb部分:

<div class="text-center col-xs-1"> 
    <% if current_user %> 
    <div class="width: 100%"><%= link_to " ", joke_up_vote_path(joke), class: 'glyphicon glyphicon-chevron-up', method: :post, style: "margin-right: 0; margin-left: 0" %></div> 
    <% else %> 
    <div class="width: 100%"><%= link_to " ", new_user_session_path, class: 'glyphicon glyphicon-chevron-up', method: :post, style: "margin-right: 0; margin-left: 0" %></div> 
    <% end %> 
    <div class="width: 100%"><h3 style="margin-top: 0; margin-bottom: 0"><strong><%= joke.points %></strong></h3></div> 
    <% if current_user %> 
    <div class="width: 100%"><%= link_to " ", joke_up_vote_path(joke), class: 'glyphicon glyphicon-chevron-down', method: :post, style: "margin-right: 0; margin-left: 0" %></div> 
    <% else %> 
    <div class="width: 100%"><%= link_to " ", new_user_session_path, class: 'glyphicon glyphicon-chevron-down', method: :post, style: "margin-right: 0; margin-left: 0" %></div> 
    <% end %> 
</div> 

是显示我otherjokes/kids.html.erb页:

<% @jokes.each do |joke| %> 
    <div class="row"> 
    <% if joke.approved == true && joke.kids == true %> 

    <%= render partial: 'votes/voter', locals: { joke: joke } %> 

    <div class="col-xs-11"> <!-- non vote container --> 

     <h2 id="joke-title" style="margin-top: 0px"> 
     <%= joke.title %> 
     <% if joke.kids == true %> 
      <%= image_tag 'icon_kids.jpg', style: "height: 25px; margin-left: 10px" %> 
     <% end %> 
     <% if joke.mixed == true %> 
      <%= image_tag 'icon_mixed.jpg', style: "height: 25px; margin-left: 10px" %> 
     <% end %> 
     </h2> 

     <p id="joke-body"><%= joke.body %></p> 
     <p><strong>Submitted by: <%= joke.user.first_name %></strong></p> 
     <% if current_user && (current_user == joke.user || current_user.admin) %> 
     <%= link_to edit_joke_path(joke) do %> 
      <span style="color: blue" class="glyphicon glyphicon-pencil" aria-hidden="true"></span><span style="color: blue">Edit Joke</span> 
     <% end %> 
     <%= link_to joke_path(joke), data: {:confirm => 'Are you sure?'}, :method => :delete do %> 
      <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>Delete Joke 
     <% end %> 
     <% end %> 
    </div> <!-- non vote container --> 
    </div> <!-- joke row --> 
    <% end %> 
    <div class="row text-center"> 
    <hr style="width: 50%; margin-top: 30px; margin-bottom: 30px"> 
    </div> <!-- row --> 

<% end %> 

我已经做了如下调整,我routes还有:

resources :jokes do 
    patch :approve, on: :member 
    patch :reject, on: :member 
    post '/up-vote' => 'votes#up_vote', as: :up_vote 
    post '/down-vote' => 'votes#down_vote', as: :down_vote 
    end 

现在,当我尝试对一个笑话进行投票时,在我的votes_controller的末尾@vote = current_user.votes.create(value: new_value, joke: @joke)行上被调用。

我很想让这个系统运行的方式,它可以应用到其他未来的模型,这将需要投票,也(如果可能)做到这一点“的Ruby方式”。任何人都可以帮助我正确地构造这个吗

回答

0

由于错误状态,joke不是Vote的属性。因为它是一个多态关联,所以你给它的通用名称votable。使用它为Joke创建一个Vote

@vote = current_user.votes.create(value: new_value, votable: @joke) 

votable_idvotable_type将自动被框架导出。

+0

这是否在'jokes_controller'或'votes_controller'?如果我把它放在笑话控制器的索引运算符中,我会在该行上得到一个错误,说'未定义的局部变量或方法'new_value'为# Liz

+0

它代替了您的votes_controller中几乎相同的行。这是错误的线。 – AmShaegar

+0

将错误更改为'undefined method'update_vote'for#'。 – Liz