chapters_controller
の強力なパラメータは、Book
エンティティとChapter
エンティティの場合はどうすればよいですか?Rails Emberの強力なパラメータ解明
注:JSON APIを使用しています。
:title, :order, :content, :published, :book, :picture
をそれとも、次のようになります:私のchapters_controller
で
、私の強力なパラメータがあるべき
:title, :order, :content, :published, :book_id, :picture
私はエンバーアプリケーションで、その後、代わりに:book_id
の:book
を使用している場合は、私が行くとき新しい章を作成するために、私はそれを作成してこの章を親書に関連付けることができますが、私のテストは失敗します:
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
私のコンソールで、Association type mismatchというエラーが表示されます。
おそらく私のライン:
book: @book
は、それを引き起こしていますか?
いずれにしても、ぼんやり感が私には:book
の私のchapters_controller
強いパラメータを使用しているはずです。
私のテストは合格していません。テストに合格するためのパラメータハッシュをどのように記述するのか分かりません。