0

所以,我有一个应用程序,允许用户投票/喜欢歌曲(使用这个gem)。但是,我希望票数较高的歌曲能够排在榜单前列。目前,它们仅按上传顺序显示。我该如何做到这一点?

歌曲#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" %>
    <span class="comments"> | <%= pluralize(song.comments.size, 'comment') %></span> | <span class="votes"><%= pluralize(song.votes.count, 'like') %></span><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>

<div class="right-side">
    <%= image_tag('delorean.jpg')%>


</div></div>
<br />

歌曲控制器.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
  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
4

1 回答 1

0

你的 Song 模型应该有一个plusminus字段或类似的东西(简单地看一下 gem,看起来像plusminus,这是赞成票和反对票的总和),它计算给定歌曲收到的赞数。然后,只需对该字段进行排序查询:

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

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

  def index
    @songs = Song.order("plusminus")
  end
于 2013-07-27T16:26:34.370 回答