我正试图在嵌套到拍卖中的评论中实现嵌套的答案。嵌套的评论与嵌套的答案:未定义的方法`答案为零:NilClass | Rails
有一个auctions.rb模型,其中:
has_many :comments, dependent: :destroy
has_many :answers, :through => :comments
一个comments.rb模型,其中:
belongs_to :auction
has_many :answers, dependent: :destroy
一个answers.rb模型,其中:
belongs_to :comment
answers_controller继承自comments_controller:
class AnswersController < CommentsController
before_action :all_answers, only: [:index, :create, :update, :destroy]
before_action :set_answer, only: [:edit, :update, :destroy]
respond_to :html, :js
# New Answer (Form)
def new
@answer = Answer.new
@comments.answers.build
end
# Create Answer
def create
@answer = @comment.answers.build(answer_params)
@answer.user_id = current_user.id
@answer.save
end
# Edit Answer
def update
@answer.update!(answer_params)
end
# Delete Answer
def destroy
@answer = Comment.find(params[:id])
@comment = @answer.comment
@answer.destroy
end
private
def all_answers
@answers = @comment.answers.all
end
def set_answer
@answer = @comment.answers.find(params[:id])
end
def answer_params
params.require(:comment).permit(:body)
end
end
的错误:
NoMethodError in Auctions#show app/views/comments/_comment.html.erb where line #20 raised: undefined method `answers' for nil:NilClass
<div class="col s12" id="answer-form" style="display:none;"></div>
</div>
<div class="row">
<div class="col s12" id="answers"><%= render @comment.answers %></div>
</div>
随着<%=渲染@ comment.answers%>我要显示的相关注释下所有现有的答案。我究竟做错了什么?
auction_controller
class AuctionsController < ApplicationController
# Index of all auctions
def index
@auctions = Auction.all
end
# Show Auction by :id
def show
@auction = Auction.find(params[:id])
# Find Seller by ID
@seller = User.find(@auction.user_id)
# Find highest bid, by finding all related bids and ordering in descending and picking the first
@highest_bid = Bid.where(auction_id: params[:id]).order("amount DESC").first
# Find product
@product = Product.find(@auction.product_id)
end
# New Auction Form
def new
@auction = Auction.new
end
# Edit Auction
def edit
@auction = Auction.find(params[:id])
end
# Create new Auction
def create
# Create new Auction
@auction = Auction.new(auction_params)
# Save Id of User (Seller)
@auction.user_id = current_user.id
# If auction was created successfully
if @auction.save
# display the created auction
redirect_to @auction, :notice => "Auction created"
else
# display Form again if unsuccessful
render 'new'
end
end
# Update existing Auction
def update
@auction = Auction.find(params[:id])
# Validation
if @auction.update(auction_params)
redirect_to @auction, :notice => "Auction updated"
else
render 'edit'
end
end
# Delete Auction
def destroy
@auction = Auction.find(params[:id])
@auction.destroy
redirect_to auctions_path, :notice => "Auction deleted"
end
private
# set required parameters for new created Auctions
def auction_params
params.require(:auction).permit(:condition, :product_name)
end
end
comments_controller
class CommentsController < ApplicationController
before_action :set_auction
before_action :all_comments, only: [:index, :create, :update, :destroy]
before_action :set_comment, only: [:edit, :update, :destroy]
respond_to :html, :js
# New Comment (Form)
def new
@comment = Comment.new
@auction.comments.build
end
# Create Comment
def create
@comment = @auction.comments.build(comment_params)
@comment.user_id = current_user.id
@comment.save
end
# Edit Comment
def update
@comment.update!(comment_params)
end
# Delete Comment
def destroy
@comment = Comment.find(params[:id])
@auction = @comment.auction
@comment.destroy
end
private
def set_auction
@auction = Auction.find(params[:auction_id])
end
def all_comments
@comments = @auction.comments.all
end
def set_comment
@comment = @auction.comments.find(params[:id])
end
def comment_params
params.require(:comment).permit(:body)
end
end
正常工作的意见。只有评论回答不起作用。
错误提出的动作是什么?新?指数?等等? –
的行为是:Auctions#show,我们可以看到您的拍卖控制器吗?看起来@comment没有定义。 –
@SimoneCarletti - 错误在拍卖的展示行为中引发。 –