0

chapters_controller의 강력한 매개 변수는 Book 엔터티와 Chapter 엔터티가있을 때 무엇이되어야합니까?레일 앰버 강력한 매개 변수 설명

참고 : 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 

내 콘솔에 연결 유형 불일치 오류가 표시됩니다.

아마도 내 라인 :

book: @book 

를 일으키는?

어느 쪽이든, 장의 느낌은 내게 chapters_controller 강력한 매개 변수에 :book을 사용해야한다고 말하고 있습니다.

내 테스트가 통과하지 못하고 테스트 통과를 위해 매개 변수 해시를 작성하는 방법을 잘 모르겠습니다.

답변

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를 준수합니다.전무로 설정, 그렇지 않으면, 관계가있는 경우에만 paramsrelationships를 추가 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 

특별 참고 :

그래서 내 새로운 테스트 샘플 datas를 위해 지금이 그 관계를 무시하는 대신 그 관계를 제거하십시오.

그럼 난과 같이 내 테스트를 호출 할 수

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