【问题标题】:Post failing with a nested resource from Ember.js to a Rails API.将嵌套资源从 Ember.js 发布到 Rails API 失败。
【发布时间】:2013-06-03 23:25:31
【问题描述】:

我有一个名为 App.Routine 的嵌套资源,其中包含多个活动。当我在这里发送帖子时是我的有效负载:

{例程:{name:testName,activities:[{name:testName},{name:testName}]}}

这会返回 500 错误:

RoutinesController#create 中的 ActiveRecord::AssociationTypeMismatch

Activity(#32627220) 预期,得到 ActiveSupport::HashWithIndifferentAccess(#33577656)

我的 Rails API 正在使用 ActiveModelSerializers:

class RoutineSerializer < ActiveModel::Serializer
  attributes :id, :name

  has_many :activities, embed: :ids
end

class RoutinesController < ApplicationController
  respond_to :json
  def create
    routine = Routine.create(params[:routine])
  end

我相信我的问题在于我如何处理我的routines_controller.rb 中的创建操作。 Rails 不喜欢我如何在例程 JSON 中返回活动的哈希值,但我想不出正确的方法来处理这个问题。

【问题讨论】:

  • 你有没有解决过这个问题。有类似的问题吗?
  • @Pedr - 不幸的是没有。 Rails 不接受我发送的 JSON 似乎确实是个问题。如果我从 App.store 中删除 {embedded: 'always'},则会为父母和孩子发送 2 个单独的 JSON 帖子,其中孩子具有“parent_id”。以这种方式提交我的 POST 是成功的,但是我只想发送 1 篇带有嵌入的父/子对象的帖子。按照这个例子https://github.com/dgeb/ember_data_example,我不知道我在做什么不同会导致错误。
  • 耻辱。感谢您的更新。
  • 终于想通了。我能够通过不将子对象嵌入到父对象中来解决问题,因此为父对象发送单独的 POST 到 API,然后为每个包含 parent_id 的子对象再次发送。这不是一个理想的解决方案,所以我回去处理嵌入式父/子 json 请求。我最初的问题确实出在我的 Rails API 上。我再次遵循@dgeb 的示例,并意识到我对强参数了解不多。值得庆幸的是,有一个Railscast!一旦我正确实施,我就可以开始了!

标签: ruby-on-rails ember.js


【解决方案1】:

我原来的问题确实出在我的 Rails API 上。我再次遵循@dgeb 的示例,并意识到我对强参数了解不多。值得庆幸的是,有一个 Railscast !一旦我正确地实施了这一点,我就可以开始了!

在我的 Gemfile 中添加了“gem strong_parameters”。然后,我在父控制器上的 #create 函数调用 update_parameters 函数,我首先在其中创建并保存父控制器,然后遍历子控制器并保存它。

来自 Dan Gebhart 的 ember 数据示例:

def permitted_params
  params.require(:contact).permit(:first_name,
                                :last_name,
                                :email,
                                :notes,
                                phone_numbers: [:id, :number])
end

def update_contact(contact)
  contact_params = permitted_params
  phone_numbers_param = contact_params.extract!(:phone_numbers)
  phone_numbers_param = phone_numbers_param[:phone_numbers]
  phone_numbers_param ||= []

  # Because updates to the contact and its associations should be atomic,
  # wrap them in a transaction.
  Contact.transaction do
    # Update the contact's own attributes first.
    contact.attributes = contact_params
    contact.save!

    # Update the contact's phone numbers, creating/destroying as appropriate.
    specified_phone_numbers = []
    phone_numbers_param.each do |phone_number_params|
      if phone_number_params[:id]
        pn = contact.phone_numbers.find(phone_number_params[:id])
        pn.update_attributes(phone_number_params)
      else
        pn = contact.phone_numbers.create(phone_number_params)
      end
      specified_phone_numbers << pn
    end
  contact.phone_numbers.each do |pn|
    pn.destroy unless specified_phone_numbers.include?(pn)
  end
  end

  # Important! Reload the contact to ensure that changes to its associations
  # (i.e. phone numbers) will be serialized correctly.
  contact.reload

  return true
  rescue
  return false
  end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多