【问题标题】:Rails Ember strong parameters clarificationRails Ember 强参数说明
【发布时间】:2016-12-12 18:31:05
【问题描述】:

如果我有一个Book 实体和一个Chapter 实体,我的chapters_controller 的强参数应该是什么?

注意:我使用的是 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 

是什么原因造成的?

无论如何,直觉告诉我应该在我的chapters_controller 强参数中使用:book

只是我的测试没有通过,我不确定如何编写参数哈希以使我的测试通过。

【问题讨论】:

    标签: ruby-on-rails unit-testing ember.js strong-parameters json-api


    【解决方案1】:

    经过几个小时的努力并查看 JSON API 文档:

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

    我注意到了,为了使用 JSON API 为实体设置 belongsTo 关系,我们需要这样做:

    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 删除该关系,而不是忽略它。

    然后我可以这样调用我的测试:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 2016-11-25
      • 2021-12-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多