acts_as_votable NoMethodError(未定义的方法`注释'对于nil:NilClass):

时间:2014-08-20 16:05:28

标签: ruby-on-rails erb voting-system

我正在尝试对我的应用中的维修点内的评论进行投票,并且我一直收到无方法错误。我已经尝试重新安排我的代码以多种不同的方式让它工作,但它没有合作。我在终端中的错误显示了这个

Parameters: {"pit_id"=>"1", "comment_id"=>"2"}
  Pit Load (0.2ms)  SELECT  "pits".* FROM "pits"  WHERE "pits"."id" = ? LIMIT 1  [["id", 1]]
Completed 500 Internal Server Error in 2ms

NoMethodError (undefined method `comments' for nil:NilClass):
  app/controllers/comments_controller.rb:22:in `upvote'

它找到了“pit id”和“comment id”,但显然有些东西显而易见。有人会更好地了解最新情况,指出它是什么。感谢。

现在是我的代码

_comment.html.erb

<div class = "well">
    <p>
    <strong>Comment:</strong>
    <%= comment.body %>
    <p>posted by: <%= comment.user.name %></p>
       <%= link_to "Upvote", pit_comment_like_path(@pit, comment), method: :put, :remote => true %>
       <%= link_to "Downvote", pit_comment_dislike_path(@pit, comment), method: :put, :remote => true %>
  </p>

  <p>
    <%if comment.user == current_user %>

     <%= link_to 'Destroy Comment', [@pit, comment],
                 method: :delete,
                 data: { confirm: 'Are you sure?' } %>
    <% end %>             
  </p>
  </div>


Comments Controller

class CommentsController < ApplicationController


 def create
  @pit= Pit.find(params[:pit_id])
  @comment = @pit.comments.build(comments_params)
  @comment.user = current_user
  @comment.save

  redirect_to pit_path(@pit)
end

  def destroy
    @pit = Pit.find(params[:pit_id])
    @comment = @pit.comments.find(params[:id])
    @comment.destroy
    redirect_to pit_path(@pit)
end

def upvote
  @comment = Pit.find(params[:pit_id])
  @comment = @pit.comments.find(params[:id])
  @comment.upvote_by current_user
  redirect_to pit_path(@pit)
end

def downvote
  @comment = Pit.find(params[:pit_id])
  @comment = @pit.comments.find(params[:id])
  @comment.downvote_by current_user
  redirect_to pit_path(@pit)
end

路线

Rails.application.routes.draw do

  devise_for :users, :controllers => { registrations: 'registrations' }
  devise_scope :user do
    get 'users/sign_in' => 'devise/sessions#new'
    get 'users/sign_out' => 'devise/sessions#destroy'
    match 'users/:id', to: 'users#show', as: 'user', via: 'get'
  end

  resources :pits do
    resources :comments do
      put "like", to: "comments#upvote"
      put "dislike", to: "comments#downvote"
    end
  end





  root to: 'pages#home'
  get '/about' => 'pages#about'
end

用户类

class User < ActiveRecord::Base
  acts_as_voter
  has_many :pits
  has_many :comments
  enum role: [:user, :vip, :admin]
  after_initialize :set_default_role, :if => :new_record?

  def set_default_role
    self.role ||= :user
  end

  def name
    name = first_name + ' ' + last_name
  end


  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,


 :recoverable, :rememberable, :trackable, :validatable
end

评论类

class Comment < ActiveRecord::Base
  acts_as_votable
  belongs_to :pit
  belongs_to :user
end

0 个答案:

没有答案