1

为什么我现在得到这个?一个新用户上传了一首歌曲,我去投票,我得到了以下信息:(这发生在我创建 staging 分支之后)

ActiveRecord::RecordNotFound (Couldn't find Song with id=attention-let-me-go-remix):
2013-08-14T06:11:31.740608+00:00 app[web.1]:   app/controllers/songs_controller.rb:13:in `vote_for'

歌曲控制器片段

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

  def extract_video

  @song = Song.find(params[:id])
  @song.YouTubeAddy.extract_video_id

  end

  def vote_for
      @song = Song.find(params[:id])
      current_user.vote_for(@song)
      @song.plusminus = @song.votes_for
      @song.save
      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

  def new_songs
    @songs = Song.order("id DESC").paginate(:page => params[:page], :per_page => 15)
    get_last_song
  end


  # GET /Songs
  # GET /Songs.json
  def index
    if params[:query].present? 
      @songs = Song.search(params)
      get_last_song
    elsif params[:genre]
      @songs = Song.tagged_with(params[:genre]).paginate(:page => params[:page], :per_page => 15)
      get_last_song
    else      
      @songs = Song.order('id').order('plusminus desc nulls last').paginate(:page => params[:page], :per_page => 15) 
      #@songs = Song.tally.paginate(:page => params[:page], :per_page => 15)
      get_last_song
    end
  end


  def get_last_song
    if params[:page].nil?
      @last_song = 0
    else
      @last_song = 15 * (params[:page].to_i - 1)
    end
  end

  # GET /Songs/1
  # GET /Songs/1.json
  def show
   @comment = Comment.new(song: @song) 
   @video_tag = YouTubeAddy.extract_video_id(@song.url)

  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)
    @song.user = current_user
    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

模式片段

 create_table "songs", force: true do |t|
    t.string   "title"
    t.string   "artist"
    t.text     "url"
    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"
    t.string   "slug"
  end
4

3 回答 3

2

您可能需要将其更改为@song = Song.find_by_url(params[:id])

于 2013-08-14T06:19:15.063 回答
1
@song = Song.find_by_slug(params[:id])

或者如果你想通过 id 找到你的歌曲,你必须在你的链接中使用投票song.id

  vote_for_song_path(song.id)

现在您可以使用:

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

在控制器中。

于 2013-08-14T21:51:14.060 回答
-1

你也可以这样做

@song = Song.all.where(:url => params[:id])

find_by_attribute 的事情在我的情况下似乎不起作用。

于 2013-08-14T06:48:43.437 回答