Rails 5 API accepts_nested_attributes_for不起作用

时间:2018-03-26 18:14:35

标签: ruby-on-rails rails-api

我正在尝试使用accepts_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" }    
    ]
}

您认为可以做什么?

1 个答案:

答案 0 :(得分:2)

布鲁诺!

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

        <?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.x.x">

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name="com.x.x.Welcome_"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity
            android:name="com.x.x.MainActivity_"
            android:screenOrientation="portrait"
            android:launchMode="singleTop"/>
        <activity
            android:name="com.x.x.ImageActivity_"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme"/>
        <activity
            android:name="com.x.x.SettingsActivity_"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme"/>
        <activity
            android:name="com.x.x.CollectionActivity_"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme"/>
        <activity
            android:name="com.x.x.HowtoActivity_"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar"/>
        <activity
            android:name="com.x.x.ABrowseACF_"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme"/>
        <activity
            android:name="com.x.x.ABrowseACC_"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme"/>
    </application>

</manifest>