编辑表单从数据库中删除记录

时间:2015-11-09 14:09:24

标签: ruby-on-rails forms ruby-on-rails-4 nested-forms nested-attributes

我有一个嵌套的表单,可以很好地保存数据库的内容。唯一的问题是,当我点击编辑时," grand-child"字段从数据库中删除,因此我必须重新输入该字段上的所有内容。正常行为是在编辑之前应该存在的内容。

对于我的表单,我使用gem cocoon嵌套表单simple_forms,使用ckeditor进行富文本编辑。

这是我点击编辑时的服务器日志:通知:SQL (0.3ms) DELETE FROM "responses" WHERE "responses"."id" = $1 [["id", 53]]每次我点击检查模型中的任何条目时都会发生这种情况。另请注意,问题已选中但未删除。很奇怪。

    Started GET "/exams/24/edit" for 127.0.0.1 at 2015-11-09 16:59:40 +0300
Processing by ExamsController#edit as HTML
  Parameters: {"id"=>"24"}
  Exam Load (0.8ms)  SELECT  "exams".* FROM "exams" WHERE "exams"."id" = $1 LIMIT 1  [["id", 24]]
  Question Load (0.4ms)  SELECT "questions".* FROM "questions" WHERE "questions"."exam_id" = $1  [["exam_id", 24]]
  Response Load (0.4ms)  SELECT  "responses".* FROM "responses" WHERE "responses"."question_id" = $1 LIMIT 1  [["question_id", 44]]
   (0.1ms)  BEGIN
  SQL (0.3ms)  DELETE FROM "responses" WHERE "responses"."id" = $1  [["id", 53]]
   (15.3ms)  COMMIT
  Rendered exams/_response_fields.html.haml (2.4ms)
  Rendered exams/_response_fields.html.haml (2.1ms)
  Rendered exams/_question_fields.html.haml (32.6ms)
  Rendered exams/_response_fields.html.haml (0.7ms)
  Rendered exams/_response_fields.html.haml (0.6ms)
  Rendered exams/_question_fields.html.haml (8.1ms)

考试模型exam.rb

belongs_to :unit

has_many :questions, :dependent => :destroy

has_many :answers, :through => :questions

accepts_nested_attributes_for :questions, :reject_if => :all_blank, :allow_destroy => true

问题模型

belongs_to :exam

has_one :response, :dependent => :destroy

accepts_nested_attributes_for :response

响应模型

class Response < ActiveRecord::Base
belongs_to :question
end

_form.html.haml部分

    = simple_form_for @exam do |f|
      - if @exam.errors.any?
        #error_explanation
          %h2= "#{pluralize(@exam.errors.count, "error")} prohibited this exam from being saved:"
          %ul
            - @exam.errors.full_messages.each do |msg|
              %li= msg

      .field
        = f.label :Exam_Title
        = f.text_field :title , size: 100
      .field
        = f.label :date
        = f.datetime_select :date

      .field
        = f.simple_fields_for :questions do |question|
          = render "question_fields", f: question
        .links
          = link_to_add_association 'Add Question', f, :questions

      .field
        =f.label :unit
        =f.select :unit_id, Unit.all.map { |u| [u.name, u.id]}


      .actions
        = f.submit 'Save'

_questions.html.haml部分

    .nested-fields
    %br/
    = f.label :question, "Question"
    = link_to_remove_association "Remove Question", f
    %br/
    = f.cktext_area :question, :cols => 30, :ckeditor => {:uiColor => '#f7931e', :toolbar => 'mini'}
    %br/
    .link
        = link_to_add_association 'Add Answer', f, :response


    = f.fields_for :response do |answer|
        =render "response_fields", f: answer

_responses.html.haml部分

.nested-fields
%br/
= f.label :response, "Answer"
= link_to_remove_association "Remove Answer", f
= f.cktext_area :answer, :cols => 10, :ckeditor => {:uiColor => '#f7931e', :toolbar => 'mini'}

更新

考试编辑页面只是呈现表单。 edit.html.haml

 %h1 Editing exam

= render 'form'

= link_to 'Show', @exam
\|
= link_to 'Back', exams_path

保存期间发送的参数:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"OvzRdRYT8zeCz6pTxcJT4o3maLb1TFw7iYxytZmnVkT7ZAG/maxeZLIoizlc8QMIMA4IrZoufh17Xkmt0NvG3A==", "exam"=>{"title"=>"Bla bla bla", "date(1i)"=>"2015", "date(2i)"=>"11", "date(3i)"=>"9", "date(4i)"=>"12", "date(5i)"=>"05", "questions_attributes"=>{"0"=>{"_destroy"=>"false", "question"=>"<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width: 500px;\">\r\n\t<tbody>\r\n\t\t<tr>\r\n\t\t\t<td>blalal</td>\r\n\t\t\t<td>nalndjksl</td>\r\n\t\t</tr>\r\n\t\t<tr>\r\n\t\t\t<td>sdfjsdljfl</td>\r\n\t\t\t<td>kjfskldjfksl</td>\r\n\t\t</tr>\r\n\t\t<tr>\r\n\t\t\t<td>kdfjkdjf</td>\r\n\t\t\t<td>gldjkgldfj</td>\r\n\t\t</tr>\r\n\t</tbody>\r\n</table>\r\n\r\n<p>htkathiuerwehjr uyewuyrhjewhr ;aueryaueh rw erkjwherw</p>\r\n", "response_attributes"=>{"_destroy"=>"false", "answer"=>"<p><img alt=\"\" src=\"/uploads/ckeditor/pictures/1/content_hand_wrinting.gif\" style=\"width: 721px; height: 370px;\" /></p>\r\n"}, "id"=>"44"}}, "unit_id"=>"4"}, "commit"=>"Save", "id"=>"24"}

在编辑期间发送的参数Parameters: {"id"=>"24"}

服务器登录加载编辑页面

  Exam Load (0.9ms)  SELECT  "exams".* FROM "exams" WHERE "exams"."id" = $1 LIMIT 1  [["id", 24]]
  Question Load (0.5ms)  SELECT "questions".* FROM "questions" WHERE "questions"."exam_id" = $1  [["exam_id", 24]]
  Response Load (0.5ms)  SELECT  "responses".* FROM "responses" WHERE "responses"."question_id" = $1 LIMIT 1  [["question_id", 44]]
   (0.2ms)  BEGIN
  SQL (0.3ms)  DELETE FROM "responses" WHERE "responses"."id" = $1  [["id", 59]]
   (12.1ms)  COMMIT
  Rendered exams/_response_fields.html.haml (2.6ms)
  Rendered exams/_response_fields.html.haml (2.3ms)
  Rendered exams/_question_fields.html.haml (44.3ms)
  Rendered exams/_response_fields.html.haml (0.7ms)
  Rendered exams/_response_fields.html.haml (0.6ms)
  Rendered exams/_question_fields.html.haml (10.1ms)

更新! exam_controller.rb

def exam_params
  params.require(:exam).permit(:title, :attachment, :date, :unit_id, 
    questions_attributes:[ :id, :question, :exam_id,  :_destroy,
    response_attributes:[:id, :answer, :question_id, :_destroy]]
    # responses:[:id, :response, :question_id, :_destroy]]
    )
end

def new
@exam = Exam.new
  1.times do
    @exam.questions.build.build_response
  end

#GET /考试/ 1 /编辑   def编辑   端

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

知道了!!

问题出在_questions.html.haml

.nested-fields
%br/
= f.label :question, "Question"
= link_to_remove_association "Remove Question", f
%br/
= f.cktext_area :question, :cols => 30, :ckeditor => {:uiColor => '#f7931e', :toolbar => 'mini'}
%br/
.link
    = link_to_add_association 'Add Answer', f, :response


= f.fields_for :response do |answer|
    =render "response_fields", f: answer

= link_to_add_association 'Add Answer', f, :response导致了问题。它会强制删除上一个答案,以便添加一个新答案,而不是按预期编辑旧答案。

答案 1 :(得分:0)

尝试在:force_non_association_create => true上添加link_to_add_association

查看this question了解详情!