Ruby on rails 充当可供选择的命名者(未定义的方法“注释”,用于nil:NilClass):
我正在尝试在我的应用程序中对pits中的评论进行投票,但我一直得到一个无方法错误。我尝试过用多种不同的方式重新排列我的代码以使其工作,但它不起作用。我在终端中的错误显示了这一点Ruby on rails 充当可供选择的命名者(未定义的方法“注释”,用于nil:NilClass):,ruby-on-rails,erb,voting-system,Ruby On Rails,Erb,Voting System,我正在尝试在我的应用程序中对pits中的评论进行投票,但我一直得到一个无方法错误。我尝试过用多种不同的方式重新排列我的代码以使其工作,但它不起作用。我在终端中的错误显示了这一点 Parameters: {"pit_id"=>"1", "comment_id"=>"2"} Pit Load (0.2ms) SELECT "pits".* FROM "pits" WHERE "pits"."id" = ? LIMIT 1 [["id", 1]] Completed 500 I
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
用户类
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用户:新建记录?
def set_默认_角色
self.role | |=:用户
结束
定义名称
姓名=名+''+姓
结束
#包括默认设计模块。其他可供选择的项目包括:
#:可确认、:可锁定、:超时和:全授权
设计:数据库可验证,可注册,
:可恢复,:可记忆,:可跟踪,:可验证
结束
评论类
class Comment < ActiveRecord::Base
acts_as_votable
belongs_to :pit
belongs_to :user
end
class注释
您没有在upvote
方法中初始化@pit
,因此这一行@comment=@pit.comments.find(params[:id])
中的nil
给出了该错误。感谢您的回复。如果我把我的upvote方法-@pit=pit.find(params[:pit\u id])@comment=@pit.comments.find(params[:id])@comment.upvote\u由当前用户重定向到pit\u路径(@pit),我会得到ActiveRecord::RecordNotFound(找不到没有id的评论):app/controllers/comments\u controller.rb:22:在我的终端的“upvote”中。所以我把:comment\u id传入了“@comment=@pit.comments.find(params[:id]),它现在正在记录我的投票,但我在2014-08-21 11:07:23-0500 PitsController处理时,在终端中得到一个模板错误,上面写着127.0.0.1的“Started PUT”/pits/1”,更新为JS参数:{“id”=>“1”}在18毫秒内完成了500个内部服务器错误”。
class Comment < ActiveRecord::Base
acts_as_votable
belongs_to :pit
belongs_to :user
end