无法销毁记录。没有找到关联

时间:2015-04-23 20:09:09

标签: ruby-on-rails ruby

我是一名学习Ruby on Rails的新生。对于我目前的任务,我必须构建一个问题功能。一切正常但删除了“问题”。

我正在尝试跟随一对一的表格助手。谢谢你的期待。

说明

  

完成QuestionsController及其相应的视图。使用复选框接受问题表单中已解决的输入。

     

在浏览器中测试您的更改。确认您可以:

     

查看所有问题的索引,   查看个别问题,   创造新问题,   编辑和更新问题,以及   删除问题。

当我去编辑一个问题,然后继续使用复选框将其删除时,会抛出此错误:

找不到名称“问题”的关联。它已经定义了吗?

这是我的控制台输出:

Started GET "/questions/1/edit" for ::1 at 2015-04-23 15:42:11 -0400
Processing by QuestionsController#edit as HTML
Parameters: {"id"=>"1"}
Completed 500 Internal Server Error in 2ms (ActiveRecord: 0.0ms)

ArgumentError (No association found for name `question'. Has it been defined yet?):
app/models/question.rb:3:in `<class:Question>'
app/models/question.rb:1:in `<top (required)>'
app/controllers/questions_controller.rb:27:in `edit'

questions_controller.rb

class QuestionsController < ApplicationController
  def index
    @questions = Question.all
  end

  def show
    @question = Question.find(params[:id])
  end

  def create
    @question = Question.new(params.require(:question).permit(:title, :body, :resolved))

    if @question.save
      flash[:notice] = "Question was saved."
      redirect_to @question
    else
      flash[:error] = "There was an error saving the question. Please try again."
      render :new
    end
  end

  def new
    @question = Question.new
  end

  def edit
    @question = Question.find(params[:id])
  end

  def update
    @question = Question.find(params[:id])
    if     @question.update_attributes(params.require(:question).permit(:title, :body, :resolved, :_destroy))
      flash[:noteice] = "Question was updated."
      redirect_to @question
    else
      flash[:error] = "There was an error saving the question. Please try again."
      render :edit
    end
  end
end

question.rb

class Question < ActiveRecord::Base
  has_many :answers
  accepts_nested_attributes_for :question, allow_destroy: true
end

edit.html.erb

<div class="col-md-8">
<%= form_for @question do |f| %>
  <div class="form-group">
    <%= f.label :title %>
    <%= f.text_field :title, class: 'form-control', placeholder: "Enter question title" %>
  </div>
  <div class="form-group">
    <%= f.label :body %>
    <%= f.text_area :body, rows: 8, class: 'form-control', placeholder: "Enter question body" %>
  </div>
  <div class="form-group">
    <%= f.label :resolved %>
    <%= f.check_box :resolved %>
  </div>
  <div class="form-group">
    <%= f.label :destroy %>
    <%= f.check_box :_destroy %>
  </div>
  <div class="form-group">
    <%= f.submit "Save", class: 'btn btn-success' %>
  </div>
<% end %>

编辑:添加了我的答案.rb。

answers.rb

class Answer < ActiveRecord::Base
  belongs_to :question
end

3 个答案:

答案 0 :(得分:3)

嵌套属性用于关系。您的Question有很多Answersaccepts_nested_attributes_for的要点是它允许你更新&#34;嵌套&#34;来自父模型的关联。参数是接受属性的关联的名称。在这种情况下,您的问题应接受答案的嵌套属性:

class Question < ActiveRecord::Base
  has_many :answers
  accepts_nested_attributes_for :answers
end

请参阅文档,其中示例类Book接受authorpages的嵌套属性。 Book 接受book的嵌套属性,类似地,您的问题不应接受问题的嵌套属性。

至于删除问题,您无法使用accepts_nested_attributes将模型指回本身,以便_destroy会破坏父模型。

您必须检查是否存在该属性并销毁控制器中的记录。

答案 1 :(得分:1)

问题是accepts_nested_attributes_for类中的Question行。

你写了

accepts_nested_attributes_for :question

但可能应该是

accepts_nested_attributes_for :answers

因为那是has_many关系的名称。 Rails正在咆哮,因为在:question类中没有称为Question的关系。你的课应该是这样的:

class Question < ActiveRecord::Base
  has_many :answers
  accepts_nested_attributes_for :answers, allow_destroy: true
end

答案 2 :(得分:-1)

你需要在answers.rb

belongs_to :question

为了完成关联