Ruby on rails 未定义的方法“为歌曲投票”路径';[第4条]
所以,我很难找到工作 我已经遵循了,但我似乎犯了以下错误: 错误消息:Ruby on rails 未定义的方法“为歌曲投票”路径';[第4条],ruby-on-rails,ruby-on-rails-3,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 4,所以,我很难找到工作 我已经遵循了,但我似乎犯了以下错误: 错误消息: NoMethodError in Songs#index Showing /Users/apane/Downloads/leap_stage/leap_stage/app/views/songs/index.html.erb where line #10 raised: undefined method `vote_for_song_songs_path' for #<#<Class:0x007fd0b602
NoMethodError in Songs#index
Showing /Users/apane/Downloads/leap_stage/leap_stage/app/views/songs/index.html.erb where line #10 raised:
undefined method `vote_for_song_songs_path' for #<#<Class:0x007fd0b60203d0>:0x007fd0b26ecdc0>
<li><%= link_to song.title, song %><br></li>
Submitted <%= time_ago_in_words(song.created_at) + " ago" %>
<span class="comments"> | <%= pluralize(song.comments.size, 'comment') %></span><br />
<%=link_to image_tag('thumbs_up', :border => 0), vote_for_song_songs_path(@song), :remote => true %>
<%=link_to image_tag('thumbs_down', :border => 0), vote_against_song_songs_path(@song), :remote => true %>
因为您需要一个:id参数,所以它应该是get或put,为了避免编写“song_songs”,您可以这样做
resources :songs do
member do
get :vote_for, :vote_against
end
end
这将获得对歌曲路径(@song)的投票和对歌曲路径(@song)的投票。从技术上讲,让put:vote_支持
更为正确,因为这不是一个幂等请求,但您需要记住将method::put
放在url的末尾
编辑:要获得要显示的投票数,请在页面中添加一个元素,如
<span class="votes"><%= pluralize(song.votes.count, 'Vote') %></span>
这将生成类似于…
的html,您可以在update\u.js.erb模板中引用它
$("#song_<%= @song.id %> .votes").html("<%= pluralize(song.votes.count, "Vote") %>")
投赞成票和投反对票的行动都可以做到这一点。这样做了,现在我得到了:未定义的方法“投赞成票,投反对票,投赞成票,投反对票路径”是正确的,因为该方法现在可以写成
投赞成票,而不是投赞成票,投反对票路径
。谢谢我的朋友,最后一件事是,当我单击“竖起大拇指”或“竖起大拇指”时,它似乎起作用了,但你知道我如何显示投票数吗?解决方案足够长,我将编辑我的答案。
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
get '/contact', to: 'songs#contact'
get '/faq', to: 'songs#faq'
root to: 'songs#index'
end
resources :songs do
member do
get :vote_for, :vote_against
end
end
<span class="votes"><%= pluralize(song.votes.count, 'Vote') %></span>
<%= content_tag_for :li, song do %>
[then put everything in your view that has to do with one song in the block]
<% end %>
$("#song_<%= @song.id %> .votes").html("<%= pluralize(song.votes.count, "Vote") %>")
format.js { render 'update_votes' }