允许在Rails中嵌套参数

时间:2016-09-03 22:47:57

标签: ruby-on-rails ruby ruby-on-rails-4 strong-parameters

我想允许长参数哈希,我试图跟随strong params docs on nested params,但我遇到了一些错误,我找不到有什么问题。

def project_params
  params.require(:project).permit(:name,
                                  :scale,
                                  :unit,
                                  :colorFormat,
                                  :artboards => [
                                    {
                                      :layers => [
                                        {
                                          :objectID, :type, :name,
                                          :rect => {
                                            :x, :y, :width, :height
                                          },
                                          :rotation, :radius, :borders => [], :fills => [], :shadows => [], :opacity, :styleName
                                        }
                                      ],
                                      :notes => [
                                        {
                                          :rect => {
                                            :x, :y, :width, :height
                                          },
                                          :note
                                        }
                                      ], :pageName, :pageObjectID, :name, :slug, :objectID, :width, :height, :imagePath
                                    }
                                  ],
                                  :slices => [
                                    :name, :objectID,
                                    :rect => {
                                      :x, :y, :width, :height
                                    },
                                    :exportable => [
                                      {
                                        :name, :density, :format, :path
                                      }
                                    ]
                                  ],
                                  :colors => [
                                  ]
                                 )
end

以下是我遇到的错误:

projects_controller.rb:60: syntax error, unexpected ',', expecting =>
...=> [ {:layers => [ { :objectID, :type, :name, :rect => {:x, ...
...                               ^
projects_controller.rb:60: syntax error, unexpected ',', expecting =>
...ID, :type, :name, :rect => {:x, :y, :width, :height}, :rotat...
...                               ^
projects_controller.rb:60: syntax error, unexpected ',', expecting =>
... } ], :notes => [{:rect => {:x, :y, :width, :height}, :note}...
...                               ^
projects_controller.rb:60: syntax error, unexpected ',', expecting =>
...:name, :objectID, :rect => {:x, :y, :width, :height}, :expor...
...                               ^
projects_controller.rb:60: syntax error, unexpected ',', expecting =>
...eight}, :exportable => [{:name, :density, :format, :path}], ...
...                               ^):

我不知道为什么它需要=>而不是,,而我只需要标量值而不是数组或散列。我在这里缺少什么?

修改

现在我修复了大部分参数:

  params.require(:project).permit(:slug,
                                  :scale,
                                  :unit,
                                  :color_format,
                                  artboards: [
                                    :page_name, :page_object_id, :name, :slug, :object_id, :width, :height, :image_path,
                                    layers: [
                                      :object_id, :type, :name, :rotation, :radius, :opacity, :style_name, :font_size, :font_face, :text_align, :letter_spacing, :line_height, :content, rect: [], css: [], borders: [], fills: [], shadows: [], color: []
                                    ],
                                    notes: [
                                      :note,
                                      rect: [
                                        :x, :y, :width, :height
                                      ]
                                    ]
                                  ],
                                  slices: [
                                    :name, :object_id,
                                    rect: [
                                      :x, :y, :width, :height
                                    ],
                                    exportable: []
                                  ],
                                  colors: []
                                 )

我得到了:

Unpermitted parameter: rect
Unpermitted parameter: rect
Unpermitted parameters: rect, fills
Unpermitted parameters: rect, color
Unpermitted parameters: rect, borders
Unpermitted parameters: rect, color
Unpermitted parameters: exportable, rect

以下是JSON的示例:https://jsonblob.com/57cb9e08e4b0dc55a4f2b785

1 个答案:

答案 0 :(得分:3)

似乎是在说,因为你在{}包裹了这些符号,它期待一个哈希,这意味着你需要一个键和值,比如:objectID => some_value

在这一行中,

{ :objectID, :type, :name, :rect => {:x, ...

期待像

这样的东西
{ :objectID => val1, :type => val2, :name => val3, :rect => {:x, ...

这是直接来自嵌套参数的强参数文档

params.permit(:name, {:emails => []}, :friends => [ :name, { :family => [ :name ], :hobbies => [] }])

请注意{}内的所有内容始终都有一个键值对。例如{:emails => []}{ :family => [ :name ], :hobbies => [] }