加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 删除嵌套属性时未允许的属性_destroy

发布时间:2020-12-17 02:46:41 所属栏目:百科 来源:网络整理
导读:关注railscast#196但使用Rails 4 – 我在尝试从问题父模型中删除答案时遇到了问题. 未允许的参数:_destroy _answer_fields.html.erb fieldset %= f.text_field :response % %= f.hidden_field :_destroy % %= link_to "remove","#",class: "remove_fields"
关注railscast#196但使用Rails 4 – 我在尝试从问题父模型中删除答案时遇到了问题.

未允许的参数:_destroy

_answer_fields.html.erb

<fieldset>                                                                                                                                                                                                  
    <%= f.text_field :response %>                                                                                                                                                                       
    <%= f.hidden_field :_destroy %>                                                                                                                                                                                                                                                                                                                                                                                 
    <%= link_to "remove","#",class: "remove_fields" %>                                                                                                                                               
</fieldset>

question.rb

accepts_nested_attributes_for :questions,:allow_destroy => true

surveys_controller.rb

def survey_params                                                                                                                                                                                       
  params.require(:survey).permit(:name,:questions_attributes => [:id,:content,:answers_attributes => [:id,:response]])                                                                              
end

我正在删除表单上的字段点击罚款,但记录尚未删除.

谢谢

编辑:JS设置隐藏变量

jQuery ->                                                                                                                                                                                                   
        $(document).on 'click',".remove_fields",(event) ->                                                                                                                                              
                $(this).prev('input[type=hidden]').val('1')                                                                                                                                                 
                $(this).closest('fieldset').hide()                                                                                                                                                          
                event.preventDefault()

解决方法

@Hitham S. AlQadheeb可能是对的 – 检查你的模型是否正确……

survey.rb

class Survey < ActiveRecord::Base
  has_many :questions,:dependent => :destroy
  accepts_nested_attributes_for :questions
end

和问题.rb

class Question < ActiveRecord::Base
  belongs_to :survey
  has_many :answers,:dependent => :destroy
  accepts_nested_attributes_for :answers,:reject_if => lambda { |a| a[:content].blank? },:allow_destroy => true
end

不允许的参数:_destroy错误实际上应该指的是你的控制器的强参数的实现:survey_params.你需要告诉rails你的表单将传递:_destroy字段.试试这个:

def survey_params                                                                                                                                                                                       
  params.require(:survey).permit(:name,:_destroy,:response,:_destroy]])                                                                              
end
---------------------------------------------------------------------------------^
-------------------------------------------------------------------------------------------------------------------------------------^

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读