大拇指'加减'是如何工作的?

时间:2013-07-28 19:34:39

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4

所以,我按照here上的说明使用plusminus列来计算投票,但它不起作用。当我点击进行投票时,会添加投票,但不会根据更高/更低的投票数进行排名。

song_controller.rb

class SongsController < ApplicationController
  before_filter :authenticate_user!, only: [:create ,:edit, :update, :destroy, :vote_for_song]
  before_action :set_song, only: [:show, :edit, :update, :destroy, :vote_for_song]


  def vote_for
      @song = Song.find(params[:id])
      current_user.vote_for(@song)
      respond_to do |format|
        format.js { render 'update_votes' }
      end
  end

  def vote_against
    @song = Song.find(params[:id])
    current_user.vote_against(@song)
    respond_to do |format|
      format.js { render 'update_votes' }
    end
  end


  # GET /Songs
  # GET /Songs.json
  def index
    @songs = Song.all.order("plusminus desc")  
  end

  # GET /Songs/1
  # GET /Songs/1.json
  def show
   @comment = Comment.new(song: @song) 
  end

  # GET /Songs/new
  def new
    @song = Song.new
  end

  # GET /Songs/1/edit
  def edit
  end

  # POST /Songs
  # POST /Songs.json
  def create
    @song = Song.new(song_params)

    respond_to do |format|
      if @song.save
        format.html { redirect_to @song, notice: 'Song was successfully created.' }
        format.json { render action: 'show', status: :created, location: @song }
      else
        format.html { render action: 'new' }
        format.json { render json: @song.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /Songs/1
  # PATCH/PUT /Songs/1.json
  def update
    respond_to do |format|
      if @song.update(song_params)
        format.html { redirect_to @song, notice: 'Song was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @song.errors, status: :unprocessable_entity }
      end
    end
  end

  # Song /Songs/1
  # Song /Songs/1.json
  def destroy
    @song.destroy
    respond_to do |format|
      format.html { redirect_to songs_url }
      format.json { head :no_content }
    end
  end

  private

    def set_song
       @song = Song.find(params[:id])
     end

     def song_params
       params.require(:song).permit(:title, :artist, :bio, :track, :user_id)
     end
  end

曲#index.html.erb

<div id="layout-1">
<!--div class="left-side"> -->

<h3>Songs</h3>
<ol>
<% @songs.each do |song| %>
 <li><%= link_to song.title, song %><br></li>

<%=link_to  '&#9650'.html_safe, vote_for_song_path(song), :remote => true, :method => :put %> 
 <%#=link_to '&#9660'.html_safe, vote_against_song_path(song), :remote => true, :method => :put %> |

    Submitted <%= time_ago_in_words(song.created_at) + " ago" %>
    <small><span class="comments"> | <%= pluralize(song.comments.size, 'comment') %></span></small> | <small><span class="votes"><%= pluralize(song.votes.count, 'like') %></span></small><br />

<%#= link_to 'Show', song, class: "button small secondary" %>
<%= link_to('Edit', edit_song_path(song), class: "button small secondary") if can? :update, song %>
<%= link_to('Destroy', song, method: :delete, data: {confirm: 'Are you sure?'}, class: "button small secondary") if can? :destroy, song %>

<% end %>

</ol>
</div>

schema.rb

ActiveRecord::Schema.define(version: 20130727163912) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "comments", force: true do |t|
    t.integer  "song_id"
    t.string   "author_name"
    t.string   "site_url"
    t.text     "content"
    t.integer  "user_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "songs", force: true do |t|
    t.string   "title"
    t.string   "artist"
    t.text     "bio"
    t.string   "track_file_name"
    t.string   "track_content_type"
    t.integer  "track_file_size"
    t.datetime "track_updated_at"
    t.integer  "user_id"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "plusminus"
  end

  create_table "users", force: true do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.boolean  "admin"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree

  create_table "votes", force: true do |t|
    t.boolean  "vote",          default: false, null: false
    t.integer  "voteable_id",                   null: false
    t.string   "voteable_type",                 null: false
    t.integer  "voter_id"
    t.string   "voter_type"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "votes", ["voteable_id", "voteable_type"], name: "index_votes_on_voteable_id_and_voteable_type", using: :btree
  add_index "votes", ["voter_id", "voter_type", "voteable_id", "voteable_type"], name: "fk_one_vote_per_user_per_entity", unique: true, using: :btree
  add_index "votes", ["voter_id", "voter_type"], name: "index_votes_on_voter_id_and_voter_type", using: :btree

end

1 个答案:

答案 0 :(得分:2)

您必须首先在plusminus字段中保存您的投票数:

 def vote_for
      @song = Song.find(params[:id])
      current_user.vote_for(@song)
      @song.plsuminus = @song.voted_for
      @song.save

      respond_to do |format|
        format.js { render 'update_votes' }
      end
  end

然后按plusminus字段的索引操作顺序:

  @songs = Song.order(:plusminus)