13

関連する質問を精査しましたが、AngularJS フロントエンドから返された JSON を介して Rails 4 のネストされた属性を更新する際にまだ問題があります。

質問:以下のコードは、Rails4 アプリの AngularJS から Candidate モデルに渡される JSON の概要を示しています。Candidate モデルには多くの Works があり、Candidate モデルを通じて Works モデルを更新しようとしています。何らかの理由で Works モデルの更新に失敗しました。不足しているものを誰かが指摘してくれることを願っています。ご協力いただきありがとうございます。


候補の AngularJS フロントエンドの json は次のとおりです。

{"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 には、簡単な更新が含まれています。

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 モデルには、works モデルとの has_many 関係を定義する次のコードが含まれています。

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 関係の反対側を定義します。

class Work < ActiveRecord::Base
  belongs_to :candidate
end

私はかなり単純なものが欠けていると確信しているので、あなたが提供できるかもしれないどんな助けにも感謝します.

ありがとう!

4

3 に答える 3

1

これを初期化子などに入れることで常にnested_attributesを含めるようにパッチパラメータのラッピングをモンキーすることもできwrap_parameters.rbます:

    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
于 2016-02-28T12:59:01.407 に答える