编辑表格删除是数据库中的记录 [英] Edit Form Delete's a record from the database

查看:177
本文介绍了编辑表格删除是数据库中的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我为嵌套表单使用 gem cocoon simple_forms 用于我的表单, ckeditor 用于richtext编辑。



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

 开始GET/ exams / 24 / editfor 127.0.0.1 at 2015-11-09 16:59 :40 +0300 
ExamsController处理#编辑为HTML
参数:{id=>24}
考试负荷(0.8ms)选择exams。* FROM考试WHEREexams。id= $ 1 LIMIT 1 [[id,24]]
问题加载(0.4ms)SELECTquestions。* FROMquestionsWHEREquestions。exam_id = $ 1 [[exam_id,24]]
响应负载(0.4ms)SELECTresponses。* FROMresponsesWHEREresponses。question_id= $ 1 LIMIT 1 [[question_id, 44]]
(0.1ms)BEGIN
SQL(0.3ms)DELETE FROMresponsesWHEREresponses。id= $ 1 [[id,53]]
15.3ms)COMMIT
提交的考试/ _response_fields.html.haml(2.4ms)
提交的考试/_response_fields.html.haml(2.1ms)
提交的考试/_question_fields.html.haml(32.6毫秒)
提交的考试/ _response_fields.html.haml(0.7毫秒)
提交的考试/ _response_fields.html.haml(0.6毫秒)
提交的考试s /_question_fields.html.haml(8.1ms)

考试模式考试。 rb

  belongs_to:单位

has_many:questions,:dependent = > :销毁

has_many:answers,:through => :问题

accept_nested_attributes_for:questions,:reject_if => :all_blank,:allow_destroy =>真

质疑模型

  belongs_to:exam 

has_one:response,:dependent => :销毁

accept_nested_attributes_for:回应

回应模式

  class Response< ActiveRecord :: Base 
belongs_to:问题
结束

_form.html.haml partial

  = simple_form_for @exam do | f | 
- if @ exam.errors.any?
#error_explanation
%h2 =#{pluralize(@ exam.errors.count,error)}禁止保存此考试:
%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 |
= renderquestion_fields,f:question
.links
= link_to_add_association'添加问题',f,:问题

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


.actions
= f.submit'保存'
$ b

_questions.html.haml partial

  .nested-fields 
%br /
= f.label:question,Question
= link_to_remove_association删除问题,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做|回答|
= renderresponse_fields,f:answer

_responses。 html.haml partial

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

更新



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

 %h1编辑考试

=渲染'表格'

= link_to'显示',@exam
\ |
= link_to'Back',exams_path

保存期间发送的参数:



pre $ 参数{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\\\
\t< TBODY> \r\\\
\t\t< TR> \r\\\
\t\t\t< TD> blalal< / TD> \r\\\
\t\t\t< TD> nalndjksl< / TD> \r\\\
\t\t< / TR> \r\\\
\t\t< TR> \r\\\
\t\t\t< TD> sdfjsdljfl< / TD> \r\\\
\t\t\ T< TD> kjfskldjfksl< / TD> \r\\\
\t\t< / TR> \r\\\
\t\t< TR> \r\\\
\t\\ \\t\t< TD> kdfjkdjf< / TD> \r\\\
\t\t\t< TD> gldjkgldfj< / TD> \r\\\
\t\t< / tr> \r\\\
\t \r\\\
\r\\\
\r\\\

htkathiuerwehjr uyewuyrhjewhr; aueryaueh rw erkjwherw< / p> \r\\\
,response_attributes=> {_ destroy=>false,answer=>< p>< img alt = \\src = \\ \\/uploads/ckeditor/pictures/1/content_hand_wrinting.gif\style = \width:721px; height = 370px; \/>< / p> \r\\\
},id=>44}},unit_id=>4 =>保存,id=>24}

参数:{id=>24}



服务器登录加载编辑页面选择考试。*从考试WHERE考试。ID= $ 1限制1 [[id,24]]
问题加载(0.5ms)SELECTquestions。* FROMquestionsWHEREquestions。exam_id= $ 1 [[exam_id,24]]
响应负载(0.5ms)SELECT响应。* FROM响应WHERE响应。question_id= $ 1 LIMIT 1 [[question_id,44]]
(0.2ms)BEGIN
SQL(0.3ms)DELETE FROMresponseWHEREresponses。id= $ 1 [[id,59]]
(12.1ms)COMMIT
渲染考试/ _response_fields.html .haml(2.6ms)
提交的考试/ _response_fields.html.haml(2.3ms)
提交的考试/ _question_fields.html.haml(44.3ms)
提交的考试/ _resp onse_fields.html.haml(0.7ms)
提交的考试/_response_fields.html.haml(0.6ms)
提交的考试/ _question_fields.html.haml(10.1ms)

更新! exam_controller.rb

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

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

end



#GET / exams / 1 / edit
def edit
end



感谢您的帮助!

解决方案

明白了!



问题出在这里 _questions.html.haml

  .nested-fields 
%br /
= f.label:question,Question
= link_to_remove_association删除问题 ,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做|回答|
= renderresponse_fields,f:answer

= link_to_add_association'Add Answer',f::response 导致了问题。它会强制删除先前的答案,以便添加新的答案,而不是像预期的那样编辑旧的答案。


I Have a nested form that works pretty well in saving content to the database. The only problem is that when I click edit, the "grand-child" field is deleted from the database and thus I have to re-enter everything on that field. The normal behavior is on editing the previous content should be present.

I am using gem cocoon for nested forms simple_forms for my forms and ckeditor for richtext editing.

This is the server log when I click edit: notice: SQL (0.3ms) DELETE FROM "responses" WHERE "responses"."id" = $1 [["id", 53]] which happens every time I click edit on any entry in the exam model. Also notice that the question is selected but not deleted. Very strange.

    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 model 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

Quesions model

belongs_to :exam

has_one :response, :dependent => :destroy

accepts_nested_attributes_for :response

Response model

class Response < ActiveRecord::Base
belongs_to :question
end

_form.html.haml partial

    = 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 partial

    .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 partial

.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'}

Update

exam edit page just renders the form. edit.html.haml

 %h1 Editing exam

= render 'form'

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

Parameters sent during save:

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 sent during edit Parameters: {"id"=>"24"}

server log on loading the edit page

  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)

Update! 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

end

# GET /exams/1/edit def edit end

Thanks for for your help!

解决方案

Got it!!

The problem was here _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

The line = link_to_add_association 'Add Answer', f, :response was causing the problem. it forces the previous answer to be removed so as to add a new one as opposed to editing the old one as expected.

这篇关于编辑表格删除是数据库中的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆