【问题标题】:Rails 5 API accepts_nested_attributes_for not worksRails 5 API 接受_nested_attributes_for 不起作用
【发布时间】:2018-03-26 18:14:35
【问题描述】:

我正在尝试使用 Accept_nested_attributes_for 保存带有任务的事件,但它不起作用。

我的模型:

class Event < ApplicationRecord
  has_many :tasks
  accepts_nested_attributes_for :tasks, :allow_destroy => true
end

class Task < ApplicationRecord
  belongs_to :event, optional: true
end

事件参数方法

params.require(:event).permit(:name, tasks_attributes: [ :name ])

通话后

{
    "name": "event",
    "tasks_attributes": [
        { "name": "Task 1" }    
    ]
}

你认为这可能是什么?

【问题讨论】:

    标签: ruby-on-rails rails-api


    【解决方案1】:

    布鲁诺!

    您实际上必须为您要发布的对象使用信封。

    {
        "event": {
            "name": "event",
            "tasks_attributes": [
                { "name": "Task 1" }    
            ]
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-01-17
      • 1970-01-01
      • 1970-01-01
      • 2011-03-04
      • 2013-12-24
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      相关资源
      最近更新 更多