【发布时间】:2015-09-19 08:11:40
【问题描述】:
我正在尝试在 cmets 中实现嵌套答案,这些答案嵌套在拍卖中。
有一个auctions.rb 模型,它:
has_many :comments, dependent: :destroy
has_many :answers, :through => :comments
一个 cmets.rb 模型,其中:
belongs_to :auction
has_many :answers, dependent: :destroy
answers.rb 模型,其中:
belongs_to :comment
answers_controller 继承自 cmets_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#show app/views/cmets/_comment.html.erb 其中第 20 行提出: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>
使用 我想在相关评论下方显示所有现有答案。我做错了什么?
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
cmets_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 - 在拍卖的显示操作中出现错误。
-
@charly-m - 我添加了拍卖控制器和 cmets 控制器。评论的定义应该没有问题(只有答案不起作用)
-
我认为您忘记在 Auction#show 中定义 @comment ,或者您没有使用正确的路线。
标签: ruby-on-rails ruby-on-rails-4