Trouble with accepts_nested_attributes_for in Rails 5.0.0.beta3, -api option

后端 未结 5 1878
野性不改
野性不改 2021-01-04 19:28

I am using Rails 5.0.0.beta3, building an API-only app using the -app option on rails new, and I am having trouble with accepts_nested_attributes_for.

In my app, a

5条回答
  •  猫巷女王i
    2021-01-04 19:46

    I am experiencing problems with accepts_nested_attributes_for as well in my Rails 5 beta 3 app and seems like it is buggy. Ideally, a bug report should be submitted, but we didn't have time to do it properly. We have the following setup:

    accepts_nested_attributes_for :attachments, allow_destroy: true
    

    Eventually, we had to monkey-patch the method inside the model like this:

      def attachments_attributes=(attributes)
        attributes.reject! do |_attachment|  
          if _attachment = Attachment.find(_attachment['id'])
            if _attachment.drop_id.nil?
              attachments << _attachment
              next true
            end
          end
          next false
        end
        # assign_nested_attributes_for_collection_association(:attachments, attributes)
      end
    

    The only thing is that the last (commented-out line) with assign_nested_attributes_for_collection_association has some issues, but hopefully this will provide you an idea how this can be fixed.

提交回复
热议问题