Rails动态嵌套属性表单未在更新

时间:2016-05-31 23:48:12

标签: jquery ruby-on-rails ruby

我有一个嵌套的属性表单,在我删除编辑后的所有相关字段后,我无法进行验证。表单代表has_many :through多对多关系。该表单适用于创建。如果我没有添加任何嵌套字段并点击提交,则验证有效。但是,当我编辑现有记录并删除所有嵌套字段然后提交时,验证不会触发,并且所有子属性都将从数据库中删除。

我需要了解会发生什么事。我还需要解决问题的方法。

这是我的模特:

expense.rb

class Expense < ApplicationRecord
  has_one :payee
  monetize :amount_cents
  has_many :expense_expense_categories, inverse_of: :expense
  has_many :expense_categories, through: :expense_expense_categories, :dependent => :destroy

  accepts_nested_attributes_for :expense_expense_categories,:allow_destroy => true

  validates_associated :expense_expense_categories
  validates :date, presence: true
  validates_numericality_of :amount, presence: true, greater_than: 0
  validates :expense_expense_category_ids, presence: true
  validates_presence_of :payee_id
end

expense_expense_category.rb

class ExpenseExpenseCategory < ApplicationRecord
  monetize :amount_cents
  belongs_to :expense, inverse_of: :expense_expense_categories
  belongs_to :expense_category, inverse_of: :expense_expense_categories

  accepts_nested_attributes_for :expense_category

  validates_numericality_of :amount, presence: true, greater_than: 0
end

expense_category.rb

class ExpenseCategory < ApplicationRecord
  has_many :expense_expense_categories,inverse_of: :expense_category
  has_many :expenses, through: :expense_expense_categories

  validates_presence_of :category
end

我将省略大部分Expense表单,只显示嵌套属性的入口点

_form.html.erb

<%= f.fields_for :expense_expense_categories do |builders| %>
  <%= render 'expense_expense_category_fields', :f => builders %>
<% end %>
<%= link_to_add_fields "Add Category", f, :expense_expense_categories %>

expense_expense_category_fields.html.erb

<div class="expense_categories_fields">
  <div class="ui form segment">
    <div class="ui grid">
      <div class="sixteen wide column">
        <%= link_to '<i class="link red large remove icon"></i>'.html_safe, "#", :class => "remove_expense_expense_categories", style:"float:right" %>
        <%= f.hidden_field :_destroy %>

        <div class="field">
          <%=f.label :amount %>
          <%= f.text_field :amount %>
        </div>

        <div class="field">
          <%=f.label :category%>
          <%= f.select :expense_category_id, ExpenseCategory.all.collect { |p| [p.category, p.id] },{prompt: "Choose Category"} %>
        </div>
      </div>
    </div>
  </div>
</div>

edit.js.coffee(我认为问题出在哪里)

$('#expense_form').html("<%= escape_javascript(render "form") %>")

$('form').on 'click', '.add_fields', (event) ->
  time = new Date().getTime()
  regexp = new RegExp($(this).data('id'), 'g')
  $(this).before($(this).data('fields').replace(regexp, time))
  event.preventDefault()

$('form').on 'click', '.remove_expense_expense_categories', (event) ->
  $(this).next('input[type=hidden]').val(true)
  $(this).closest('.expense_categories_fields').hide()
  event.preventDefault()

隐藏费用类别字段不会将其从params中删除,因此仍然随表单一起提交。但是..因为_destroy隐藏字段设置为true,所以记录被删除。我已经尝试禁用字段,以防止它们与表单一起提交,但这似乎也没有触发任何验证错误。

这就是它的样子:

$(this).siblings("div.field").find('input').attr('disabled', true)
$(this).siblings("div.field").find('select').attr('disabled',true)
$(this).closest('.expense_categories_fields').hide()

所以我被困在一个破碎的形式

0 个答案:

没有答案