ruby-on-rails - Rubocop JSON : Align the parameters of a method call if they span more than one line

标签 ruby-on-rails json ruby rubocop ruby-style-guide

我的测试文件中出现 Rubocop 问题。首先,这是我现在的代码:

should 'should show user' do
  get user_url(@user),
    headers: @header
  assert_response :success
end

should 'should update user' do
  patch user_url(@user),
    params: {
      data: {
        attributes: {
          name: @user.name
        }
      }
    }, headers: @header
  assert_response :success
end

这是 Rubocop 错误输出:

test/controllers/users_controller_test.rb:34:9: C: Align the parameters of a method call if they span more than one line.
        headers: @header
        ^^^^^^^^^^^^^^^^
test/controllers/users_controller_test.rb:40:9: C: Align the parameters of a method call if they span more than one line.
        params: { ...
        ^^^^^^^^^

所以我在样式指南中搜索了 JSON 的正确对齐方式。我真的尝试了缩进和换行的每一种组合,但 Rubocop 每次都会抛出同样的错误。 Andy 顺便说一下,将整个 JSON 放在一行中也不是解决方案。 任何人都可以解释一下正确的对齐方式如何让 Rubocop 满意吗?

最佳答案

改成

should 'should show user' do
  get user_url(@user),
      headers: @header
  assert_response :success
end

should 'should update user' do
  patch user_url(@user),
        params: {
          data: {
            attributes: {
              name: @user.name
            }
          }
        },
        headers: @header
  assert_response :success
end

解释:

因为 user_url(@user) 是您要获取第二个参数的第一个参数 headers: @header 应该与之对齐

同样适用于你有三个参数的第二名

关于ruby-on-rails - Rubocop JSON : Align the parameters of a method call if they span more than one line,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39530324/

相关文章:

ruby-on-rails - 为什么我得到 "Thin adapter not found"?

ruby-on-rails - 根据模型状态更改 URL 的逻辑应该位于 Controller 中还是模型中?

json - 错误 : googleapi: Error 400: Field CONSENTS is type RECORD but has no schema, 无效

javascript - 缺少通过 json 对象的对象循环

ruby - 如何使用 watir 滚动网页

ruby-on-rails - 如何在 Ruby 中遍历给定一周的每一天?

java - 将 JSON 字符串解析为 Java Double 时出现 NumberFormatException

jquery - 持续检查时间

ruby-on-rails - 具有高级关系的 Rails 5 RESTful API

ruby-on-rails - will_paginate、包含以及按连接表排序