在我看来,试图使用 get_vote.size 获取投票总数并获得一个未定义的get_vote方法



几乎就是我标题中所说的。我正在使用充当可投票的宝石,并且从我的终端,代码正在记录投票,并将其存储在投票表中。当我选择赞成按钮时,它会在尝试总结帖子中评论的赞成票时出现未定义get_vote的错误。

我正在尝试设置它,以便人们可以对帖子的评论投赞成票或反对票。

我的路线

Rails.application.routes.draw do
devise_for :users
# For details on the DSL available within this file, see 
http://guides.rubyonrails.org/routing.html
resources :posts do
resources :comments
member do
put "like", to: "comments#upvote"
put "dislike", to: "comments#downvote"
end
end
root 'posts#index'
end

用户模型

class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :posts
has_many :comments, through: :posts
acts_as_voter
end

注释模型

class Comment < ApplicationRecord
validates :user_id, presence: true
belongs_to :user
belongs_to :post
acts_as_votable
end

景观

<div class="container">

<p>Title: <%= @post.title %></p></br>
<p>Category: <%= @post.category %></p></br>
<p>Body: <%= @post.body %></p></br>
<p>Date Created: <%= @post.created_at.strftime("%B %d, %Y") %></p></br>
<%= link_to 'Home', root_path %>
<%= link_to 'Edit', edit_post_path(@post) %>
<%= link_to 'Delete', @post, method: :delete, data: {confirm: "Are you sure you want to delete #{@post.title}?"} %>
<% if user_signed_in?  %>
<%= render :partial => "comments/comment" %>
<% else %>
<%= "Please sign up or sign in to leave a comment." %>
<% end %>
</div>
<br />
<div class="container">
<table class="table table-striped">
<tbody>
<% @post.comments.each do |f| %>
<tr>
<td><%= link_to like_post_path(@post), class: "like", method: :put do %>
<i class="fa fa-angle-up"></i>
<%= @post.get_upvotes.size %>
<% end %>
</td>
<td><%= f.text %></td>
<td><%= f.try(:user).try(:username) %></td>
<td><%= f.created_at %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<div>
<%=  @comment.try(:user).try(:username) %>
</div>

我的评论控制器

class CommentsController < ApplicationController
before_action :upvote
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.new(comment_params)
@comment.user = current_user
if @comment.save
redirect_to @post
else
redirect_to @post
end
end
def destroy
@post = Post.find(params[:id])
@comment = @post.comments.find(params[:id])
@comment.destroy
redirect_to @post
end
def upvote
@post = Post.find(params[:id])
@comment = @post.comments.find(params[:id])
@comment.upvote_from current_user
redirect_to @post
end
def downvote
@post = Post.find(params[:id])
@comment = @post.comments.find(params[:id])
@comment.downvote_from current_user
redirect_to @post
end
private
def comment_params
params.require(:comment).permit(:text, :user_id, :username)
end
end

以防万一这些是我的路线

Prefix Verb   URI Pattern                                 Controller#Action
new_user_session GET    /users/sign_in(.:format)                    devise/sessions#new
user_session POST   /users/sign_in(.:format)                    devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format)                   devise/sessions#destroy
new_user_password GET    /users/password/new(.:format)               devise/passwords#new
edit_user_password GET    /users/password/edit(.:format)              devise/passwords#edit
user_password PATCH  /users/password(.:format)                   devise/passwords#update
PUT    /users/password(.:format)                   devise/passwords#update
POST   /users/password(.:format)                   devise/passwords#create
cancel_user_registration GET    /users/cancel(.:format)                     devise/registrations#cancel
new_user_registration GET    /users/sign_up(.:format)                    devise/registrations#new
edit_user_registration GET    /users/edit(.:format)                       devise/registrations#edit
user_registration PATCH  /users(.:format)                            devise/registrations#update
PUT    /users(.:format)                            devise/registrations#update
DELETE /users(.:format)                            devise/registrations#destroy
POST   /users(.:format)                            devise/registrations#create
post_comments GET    /posts/:post_id/comments(.:format)          comments#index
POST   /posts/:post_id/comments(.:format)          comments#create
new_post_comment GET    /posts/:post_id/comments/new(.:format)      comments#new
edit_post_comment GET    /posts/:post_id/comments/:id/edit(.:format) comments#edit
post_comment GET    /posts/:post_id/comments/:id(.:format)      comments#show
PATCH  /posts/:post_id/comments/:id(.:format)      comments#update
PUT    /posts/:post_id/comments/:id(.:format)      comments#update
DELETE /posts/:post_id/comments/:id(.:format)      comments#destroy
like_post PUT    /posts/:id/like(.:format)                   comments#upvote
dislike_post PUT    /posts/:id/dislike(.:format)                comments#downvote
posts GET    /posts(.:format)                            posts#index
POST   /posts(.:format)                            posts#create
new_post GET    /posts/new(.:format)                        posts#new
edit_post GET    /posts/:id/edit(.:format)                   posts#edit
post GET    /posts/:id(.:format)                        posts#show
PATCH  /posts/:id(.:format)                        posts#update
PUT    /posts/:id(.:format)                        posts#update
DELETE /posts/:id(.:format)                        posts#destroy
root GET    /                                           posts#index

这是我遇到的错误

NoMethodError at/posts/1 # 的未定义方法 'get_upvotes'

屏幕截图以及错误

看起来您已将Comment设置为acts_as_votable,但您正在Post实例上调用get_upvotes。您是否包括acts_as_votablePost

(另外,您发布的错误消息似乎遗漏了一些有关接收get_upvotes的详细信息(

最新更新