Rails 4不通过JSON更新嵌套属性 [英] Rails 4 Not Updating Nested Attributes Via JSON

查看:130
本文介绍了Rails 4不通过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.候选模型有很多Works,我正在尝试通过候选模型更新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"}}


候选人控制器.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


候选人.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中参考此 issue 来查看是否/何时解决了此问题

Refer to this issue in Rails to see if/when they fix this problem.

更新10/17
在此处进行PR合并: rails/rails#19254 .

Update 10/17
Pending a PR merge here: rails/rails#19254.

这篇关于Rails 4不通过JSON更新嵌套属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆