嵌套答案的嵌套注释:nil的未定义方法`answers':NilClass |导轨

时间:2015-09-19 08:11:40

标签: ruby-on-rails ruby-on-rails-4

我正在尝试将嵌套答案实施到评论中,这些评论嵌套在拍卖中。

有auction.rb模型,其中:

has_many :comments, dependent: :destroy
has_many :answers, :through => :comments

comments.rb模型,其中:

belongs_to :auction
has_many :answers, dependent: :destroy

答案.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#show app / views / comments / _comment.html.erb   第20行引出:nil的未定义方法`answers':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>

使用&lt;%= render @ comment.answers%&gt;我想在相关评论下方显示所有现有答案。我做错了什么?

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

正常评论工作。只有评论答案不起作用。

1 个答案:

答案 0 :(得分:0)

错误发生在Auctions#show,错误清楚地告诉您,您正尝试在nil对象上调用answers。因此,这意味着@comment在该视图中为零。

事实上,如果您检查show操作,则永远不会将任何对象提取/分配给@comment

  # 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

要解决此问题,请确保@comment已正确分配给Comment个实例。