简体   繁体   English

在Rails中允许嵌套参数

[英]Permitting nested parameters in Rails

I want to permit a long params hash, I tried to follow strong params docs on nested params , but I got a few errors, I can't find what is wrong. 我想允许一个长的params哈希,我试图在嵌套params上遵循强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

Here are the errors I got: 这是我得到的错误:

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}], ...
...                               ^):

I don't know why it's expecting => instead of , while I just need a scalar value not an array or hash. 我不知道为什么期望=>而不是,而我只需要一个标量值而不是数组或哈希。 What I am missing here? 我在这里想念的是什么?

EDIT 编辑

Now I fixed most of the params: 现在,我修复了大多数参数:

  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: []
                                 )

I got: 我有:

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

Here is a sample of a JSON: https://jsonblob.com/57cb9e08e4b0dc55a4f2b785 这是JSON的示例: https : //jsonblob.com/57cb9e08e4b0dc55a4f2b785

it seems to be saying because you wrapped those symbols in {} , it is expecting a hash, which means you need to have a key and value, like :objectID => some_value 这似乎是因为您将这些符号包装在{} ,所以需要一个哈希,这意味着您需要一个键和值,例如:objectID => some_value

In this line, 在这一行,

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

it is expecting something like 期待像

{ :objectID => val1, :type => val2, :name => val3, :rect => {:x, ...

This is taken straight from strong parameter docs on nested parameters, 这直接取自有关嵌套参数的强大参数文档,

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

notice that everything inside {} always have an key-value pair. 请注意, {}中的所有内容始终都有一个键值对。 such as {:emails => []} , and { :family => [ :name ], :hobbies => [] } 例如{:emails => []}{ :family => [ :name ], :hobbies => [] }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM