Ruby on rails ActiveRecord::RecordNotFound(找不到id=投票的歌曲)[rails4]
所以,我很难竖起大拇指 当我单击向上箭头向上或向下投票时,我似乎在rails日志中获得以下消息: 错误日志:Ruby on rails ActiveRecord::RecordNotFound(找不到id=投票的歌曲)[rails4],ruby-on-rails,ruby-on-rails-3,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 4,所以,我很难竖起大拇指 当我单击向上箭头向上或向下投票时,我似乎在rails日志中获得以下消息: 错误日志: I, [2013-07-24T19:49:39.719359 #13941] INFO -- : Started PUT "/songs//vote_for" for 127.0.0.1 at 2013-07-24 19:49:39 -0400 I, [2013-07-24T19:49:39.719444 #13941] INFO -- : Started PUT "/songs//
I, [2013-07-24T19:49:39.719359 #13941] INFO -- : Started PUT "/songs//vote_for" for 127.0.0.1 at 2013-07-24 19:49:39 -0400
I, [2013-07-24T19:49:39.719444 #13941] INFO -- : Started PUT "/songs//vote_for" for 127.0.0.1 at 2013-07-24 19:49:39 -0400
F, [2013-07-24T19:49:39.729009 #13941] FATAL -- :
ActiveRecord::RecordNotFound (Couldn't find Song with id=vote_for):
app/controllers/songs_controller.rb:87:in `set_song'
index.html.erb
<div id="layout1">
<h3>Songs</h3>
<ol>
<% @songs.each do |song| %>
<li><%= link_to song.title, song %><br></li>
<%=link_to '▲'.html_safe, vote_for_song_path(@song), :remote => true, :method => :put %>
<%=link_to '▼'.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, 'vote') %></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>
<br />
</div>
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 = 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 = 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
class Song < ActiveRecord::Base
acts_as_voteable
belongs_to :user
has_many :comments, :dependent => :destroy
has_attached_file :track,
:url => "/assets/songs/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/songs/:id/:style/:basename.:extension"
validates_attachment :track, :presence => true
validates :title, length: { minimum: 10 }
validates :bio, length: { maximum: 300 }
end
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :songs
has_many :comments
acts_as_voter
end
class Vote < ActiveRecord::Base
scope :for_voter, lambda { |*args| where(["voter_id = ? AND voter_type = ?", args.first.id, args.first.class.base_class.name]) }
scope :for_voteable, lambda { |*args| where(["voteable_id = ? AND voteable_type = ?", args.first.id, args.first.class.base_class.name]) }
scope :recent, lambda { |*args| where(["created_at > ?", (args.first || 2.weeks.ago)]) }
scope :descending, lambda { order("created_at DESC") }
belongs_to :voteable, :polymorphic => true
belongs_to :voter, :polymorphic => true
attr_accessible :vote, :voter, :voteable if ActiveRecord::VERSION::MAJOR < 4
# Comment out the line below to allow multiple votes per user.
validates_uniqueness_of :voteable_id, :scope => [:voteable_type, :voter_type, :voter_id]
end
Leap2::Application.routes.draw do
resources :comments
devise_for :users, controllers: {registrations: 'registrations'}
resources :songs do
member do
put :vote_for, :vote_against
end
end
get '/contact', to: 'songs#contact'
get '/faq', to: 'songs#faq'
root to: 'songs#index'
在
index.html.erb
的这两行中,将@song
替换为song
,它应该可以工作:
<%=link_to '▲'.html_safe, vote_for_song_path(@song), :remote => true, :method => :put %>
<%=link_to '▼'.html_safe, vote_against_song_path(@song), :remote => true, :method => :put %> |
true,:method=>:put%>
true,:method=>:put%>|
这两条线的情况也相同:
<%= 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 %>
在
index.html.erb
的这两行中,将@song
替换为song
,它应该可以工作:
<%=link_to '▲'.html_safe, vote_for_song_path(@song), :remote => true, :method => :put %>
<%=link_to '▼'.html_safe, vote_against_song_path(@song), :remote => true, :method => :put %> |
true,:method=>:put%>
true,:method=>:put%>|
这两条线的情况也相同:
<%= 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 %>
谢谢你,伙计。我可以向上投票,现在可以了:)但当我点击向下箭头时,什么都没有发生。为什么?没问题-很乐意帮忙。在单击向下箭头的同时查看开发日志,查看浏览器是否发送了正确的远程请求。如果不是,则链接还有其他问题。如果是,请注意提供线索的任何错误或信息消息。如果您看到有人投了反对票,请在rails控制台中查看投票数据在您投反对票时是否发生了变化。这将有助于隔离请求中是否存在问题,或者在控制器操作中的投票反对呈现“更新投票”过程中是否可能出现错误。这是:F,[2013-07-24T22:06:34.633751#14378]致命--:ActiveRecord::RecordInvalid(验证失败:Voteable已经被使用):app/controllers/songs\u controller.rb:16:in'vote\u contain'Ah-好消息是,这不是一个bug!从文档中可以看出:“默认情况下,拇指支持每个用户只允许一次投票。这可以通过删除来更改…”请看这里:谢谢我的朋友。我可以投赞成票了,现在它可以工作了:)但当我点击向下箭头时,什么也没发生。为什么?没问题-很乐意帮忙。在单击向下箭头的同时查看开发日志,查看浏览器是否发送了正确的远程请求。如果不是,则链接还有其他问题。如果是,请注意提供线索的任何错误或信息消息。如果您看到有人投了反对票,请在rails控制台中查看投票数据在您投反对票时是否发生了变化。这将有助于隔离请求中是否存在问题,或者在控制器操作中的投票反对呈现“更新投票”过程中是否可能出现错误。这是:F,[2013-07-24T22:06:34.633751#14378]致命--:ActiveRecord::RecordInvalid(验证失败:Voteable已被采用):app/controllers/songs_controller.rb:16:in'vote_contain'Ah-好消息是,这不是一个bug!从文档中可以看出:“默认情况下,ThumbsUp只允许每个用户进行一次投票。这可以通过删除…”来更改