首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >试图在get_vote.size中获得总票数,并为get_vote获得一个未定义的方法

试图在get_vote.size中获得总票数,并为get_vote获得一个未定义的方法
EN

Stack Overflow用户
提问于 2018-08-10 12:51:35
回答 1查看 34关注 0票数 0

就像我标题里写的一样。我正在使用法案作为可投票的创业板,从我的终端,代码正在记录投票,它被存储在选票表中。当我选择“向上投票”按钮时,当我试图在帖子中对评论进行总结时,它会给出未定义的get_vote的错误。

我试图设置它,以便人们可以向上投票或否决一个评论在一个帖子。

我的路线

代码语言:javascript
复制
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

用户模型

代码语言:javascript
复制
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

注释模型

代码语言:javascript
复制
class Comment < ApplicationRecord
  validates :user_id, presence: true
  belongs_to :user
  belongs_to :post
  acts_as_votable
end

视图

代码语言:javascript
复制
<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>

我的评论控制器

代码语言:javascript
复制
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

,以防万一这是我的路线

代码语言:javascript
复制
 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 /post/1未定义方法`get_upvotes‘for #

截图也是错误的

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-10 13:16:59

看起来您已经将Comment设置为acts_as_votable,但是您正在对Post的一个实例调用get_upvotes。你包括acts_as_votablePost了吗?

(而且,您发布的错误消息似乎忽略了接收get_upvotes的一些细节)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51786963

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档