0

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強いパラメータを使用しているはずです。

私のテストは合格していません。テストに合格するためのパラメータハッシュをどのように記述するのか分かりません。

答えて

0
闘争の数より時間後

とJSONのAPIドキュメントを見て:JSON APIを使用したエンティティにbelongsToの関係を設定するために、それは私の注意に来た

http://jsonapi.org/format/#crud-creating

、我々

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" } 
     } 
    } 
    } 
} 

これは私が修正できなかった過去の別の問題を解決することにもつながりました。複数のジャンルで書籍を作成できます。

"data": [ 
    { "type": "comments", "id": "5" }, 
    { "type": "comments", "id": "12" } 
] 

Additonally、私のコントローラでは、何も強い:

BookエンティティにGenreの配列を割り当てるためのJSON APIの構造は、我々はこのような関係部分のデータ配列でデータのハッシュを置き換えますそのようなパラメータ:

: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 

特記事項 - 関係がある場合にのみparamsrelationshipsを追加し、そうでない場合は、nilにそれを設定するとJSON APIを語っていますその関係を無視するのではなく、削除します。

それから私はそうのように私のテストを呼び出すことができます。

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 
関連する問題