我正在为这样的功能寻找正确的解决方案:
Book的型号Author:name、:biography、:picture等)。不希望有人为我编写现成的代码,但如果有人告诉我解决这个问题的正确方法,我将非常感激。
发布于 2016-05-04 13:41:33
您需要在Book模型中添加以下内容
belongs_to :author
accepts_nested_attributes_for :author现在,在您的控制器中,您需要允许author_id和author_attributes的params
如果将:name, :biography, :picture属性添加到author_attributes中,那么它将创建一个新的作者,并在Book实例中保存新的author_id。
如果指定author_id,则不要指定author_attributes。
发送给控制器的样本params
{ "book":
{"name": "Sample Book Title",
"author_attributes":
{ name: "John Doe", biography: "Lorem Ipsum Dolor Sir Amet",
picture: "xxx.png"}
}
},这将创建一个新用户。
或者这个
{ "book":
{"name": "Sample Book Title",
"author_id": 2
}
}这将指定作者与id 2的书。
祝好运。
https://stackoverflow.com/questions/37029076
复制相似问题