如果我有chapters_controller
实体和Book
实体,我的Chapter
的强参数应该是什么?
注意:我使用的是JSON API。
在我的chapters_controller
中,我的强参数应该是:
:title, :order, :content, :published, :book, :picture
或者它应该是:
:title, :order, :content, :published, :book_id, :picture
如果我使用:book
而不是:book_id
,那么在我的Ember应用程序中,当我创建新章节时,我能够创建它并将本章与父书相关联, ,我的测试失败了:
def setup
@book = books(:one)
@new_chapter = {
title: "Cooked Wolf Dinner",
order: 4,
published: false,
content: "The bad wolf was very mad. He was determined to eat the little pig so he climbed down the chimney.",
book: @book
}
end
def format_jsonapi(params)
params = {
data: {
type: "books",
attributes: params
}
}
return params
end
...
test "chapter create - should create new chapter assigned to an existing book" do
assert_difference "Chapter.count", +1 do
post chapters_path, params: format_jsonapi(@new_chapter), headers: user_authenticated_header(@jim)
assert_response :created
json = JSON.parse(response.body)
attributes = json['data']['attributes']
assert_equal "Cooked Wolf Dinner", attributes['title']
assert_equal 4, attributes['order']
assert_equal false, attributes['published']
assert_equal @book.title, attributes['book']['title']
end
end
我在控制台中收到错误,指出关联类型不匹配。
也许是我的专栏:
book: @book
引起了什么?
无论哪种方式,直觉都告诉我,我应该在:book
强参数中使用chapters_controller
。
这只是我的测试没有通过,我不知道如何为我的测试写入参数hash来传递。
答案 0 :(得分:0)
经过几个小时的努力并查看JSON API文档:
http://jsonapi.org/format/#crud-creating
我注意到,为了将belongsTo关系设置为具有JSON API的实体,我们需要这样做:
POST /photos HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json
{
"data": {
"type": "photos",
"attributes": {
"title": "Ember Hamster",
"src": "http://example.com/images/productivity.png"
},
"relationships": {
"photographer": {
"data": { "type": "people", "id": "9" }
}
}
}
}
这也让我解决了我过去遇到的另一个问题,我无法解决这个问题。可以使用多种类型创建书籍。
用于将Genre
数组分配给Book
实体的JSON API结构,我们将数据哈希替换为关系部分中的数据数组,如下所示:
"data": [
{ "type": "comments", "id": "5" },
{ "type": "comments", "id": "12" }
]
另外,在我的控制器中,任何强大的参数都是这样的:
:title, :content, genre_ids: []
变为
:title, :content, :genres
遵守JSON API。
因此,对于我现在的新测试样本数据:
def setup
...
@new_chapter = {
title: "Cooked Wolf Dinner",
order: 4,
published: false,
content: "The bad wolf was very mad. He was determined to eat the little pig so he climbed down the chimney.",
}
...
end
def format_jsonapi(params, book_id = nil)
params = {
data: {
type: "chapters",
attributes: params
}
}
if book_id != nil
params[:data][:relationships] = {
book: {
data: {
type: "books",
id: book_id
}
}
}
end
return params
end
关于关系设置的特别说明 - 如果存在关系,则仅将relationships
添加到params
,否则,将其设置为nil会告诉JSON API删除该关系,而不是忽略它。< / p>
然后我可以像这样调用我的测试:
test "chapter create - should create new chapter assigned to an existing book" do
assert_difference "Chapter.count", +1 do
post chapters_path, params: format_jsonapi(@new_chapter, @book.id), headers: user_authenticated_header(@jim)
assert_response :created
json = JSON.parse(response.body)
attributes = json['data']['attributes']
assert_equal "Cooked Wolf Dinner", attributes['title']
assert_equal 4, attributes['order']
assert_equal false, attributes['published']
assert_equal @book.id, json['data']['relationships']['book']['data']['id'].to_i
end