Rails 4 Not Updating Nested Attributes Via JSON

前端 未结 3 1867
囚心锁ツ
囚心锁ツ 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:58

    I figured out one way to resolve my issue based on the rails documentation at: http://edgeapi.rubyonrails.org/classes/ActionController/ParamsWrapper.html

    Basically, Rails ParamsWrapper is enabled by default to wrap JSON from the front-end with a root element for consumption in Rails since AngularJS does not return data in a root wrapped element. The above documentation contains the following:

    "On ActiveRecord models with no :include or :exclude option set, it will only wrap the parameters returned by the class method attribute_names."

    Which means that I must explicitly include nested attributes with the following statement to ensure Rails includes all of the elements:

    class CandidatesController < ApplicationController
    
        before_filter :authenticate_user!
        respond_to :json
        wrap_parameters include: [:id, :nickname, :works_attributes]
        ...
    

    Please add another answer to this question if there is a better way to pass JSON data between AngularJS and Rails

提交回复
热议问题