Rails 4 Not Updating Nested Attributes Via JSON

前端 未结 3 1851
囚心锁ツ
囚心锁ツ 2021-01-04 09:23

I\'ve scoured related questions and still have a problem updating nested attributes in rails 4 through JSON returned from my AngularJS front-end.

Question:

3条回答
  •  执念已碎
    2021-01-04 09:44

    You can also monkey patch parameter wrapping to always include nested_attributes by putting this into eg wrap_parameters.rb initializer:

        module ActionController
            module ParamsWrapper
    
                Options.class_eval do
                    def include
                        return super if @include_set
    
                        m = model
                        synchronize do
                            return super if @include_set
                            @include_set = true
                            unless super || exclude
                                if m.respond_to?(:attribute_names) && m.attribute_names.any?
                                    self.include = m.attribute_names + nested_attributes_names_array_of(m)
                                end
                            end
                        end
                    end
    
                    private 
                        # added method. by default code was equivalent to this equaling to []
                        def nested_attributes_names_array_of model
                            model.nested_attributes_options.keys.map { |nested_attribute_name| 
                                nested_attribute_name.to_s + '_attributes' 
                            }
                        end
                end
    
            end
        end
    

提交回复
热议问题