Rails 4 不通过 JSON 更新嵌套属性 [英] Rails 4 Not Updating Nested Attributes Via JSON
问题描述
我已经搜索了相关问题,但仍然无法通过从我的 AngularJS 前端返回的 JSON 更新 rails 4 中的嵌套属性.
I've scoured related questions and still have a problem updating nested attributes in rails 4 through JSON returned from my AngularJS front-end.
问题: 下面的代码概述了从 AngularJS 传递到我的 Rails4 应用程序中的 Candidate 模型的 JSON.Candidate 模型有很多 Works,我正在尝试通过 Candidate 模型更新 Works 模型.出于某种原因,Works 模型无法更新,我希望有人能指出我遗漏了什么.感谢您的帮助.
Question: The code below outlines JSON passed from AngularJS to the Candidate model in my Rails4 app. The Candidate model has many Works, and I'm trying to update the Works model through the Candidate model. For some reason the Works model fails to update, and I'm hoping someone can point out what I'm missing. Thanks for your help.
这是候选人的 AngularJS 前端中的 json:
Here's the json in the AngularJS front-end for the candidate:
{"id"=>"13", "nickname"=>"New Candidate", "works_attributes"=>[
{"title"=>"Financial Analyst", "description"=>"I did things"},
{"title"=>"Accountant", "description"=>"I did more things"}]}
<小时>
Rails 然后通过添加候选头将这个 JSON 转换为以下内容,但不包括候选头下的嵌套属性,并且无法通过候选模型更新works_attributes:
{"id"=>"13", "nickname"=>"New Candidate", "works_attributes"=>[
{"title"=>"Financial Analyst", "description"=>"I did things"},
{"title"=>"Accountant", "description"=>"I did more things"}],
"candidate"=>{"id"=>"13", "nickname"=>"New Candidate"}}
<小时>
Candidate_controller.rb 包含一个简单的更新:
The candidate_controller.rb contains a simple update:
class CandidatesController < ApplicationController
before_filter :authenticate_user!
respond_to :json
def update
respond_with Candidate.update(params[:id], candidate_params)
end
private
def candidate_params
params.require(:candidate).permit(:nickname,
works_attributes: [:id, :title, :description])
end
end
<小时>
Candidate.rb 模型包含以下代码,用于定义与作品模型的 has_many 关系:
The candidate.rb model includes the following code defining the has_many relationship with the works model:
class Candidate < ActiveRecord::Base
## Model Relationships
belongs_to :users
has_many :works, :dependent => :destroy
## Nested model attributes
accepts_nested_attributes_for :works, allow_destroy: true
## Validations
validates_presence_of :nickname
validates_uniqueness_of :user_id
end
<小时>
最后,works.rb 模型定义了 has_many 关系的另一面:
And finally, the works.rb model defines the other side of the has_many relationship:
class Work < ActiveRecord::Base
belongs_to :candidate
end
感谢您提供的任何帮助,因为我确信我遗漏了一些相当简单的东西.
I appreciate any help you may be able to provide as I'm sure that I'm missing something rather simple.
谢谢!
推荐答案
我还一直在使用 Rails 和 AngularJS 之间的 JSON API.我使用了与 RTPnomad 相同的解决方案,但找到了一种不必对包含属性进行硬编码的方法:
I've also been working with a JSON API between Rails and AngularJS. I used the same solution as RTPnomad, but found a way to not have to hardcode the include attributes:
class CandidatesController < ApplicationController
respond_to :json
nested_attributes_names = Candidate.nested_attributes_options.keys.map do |key|
key.to_s.concat('_attributes').to_sym
end
wrap_parameters include: Candidate.attribute_names + nested_attributes_names,
format: :json
# ...
end
在 Rails 中参考这个问题,看看他们是否/何时解决了这个问题.
Refer to this issue in Rails to see if/when they fix this problem.
更新 10/17
在此处等待 PR 合并:rails/rails#19254一>.
这篇关于Rails 4 不通过 JSON 更新嵌套属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!