Rails Params:需要密钥,然后允许散列数组

时间:2019-03-13 00:33:47

标签: ruby-on-rails parameters

有人可以帮我弄清楚如何要求和允许一组看起来像这样的参数:

<ActionController::Parameters { 
    "people"=>[
      {"id"=>"1", "first"=>"Jane", "last"=>"Doe"},
      {"id"=>"2", "first"=>"John", "last"=>"Doe"}
    ]
} permitted: false>

正在使用Javascript编写数据,其内容如下:

const formData = new FormData()
people.forEach(person => {
    formData.append('people[][id]', person.id)
    formData.append('people[][first]', person.first)
    formData.append('people[][last]', person.last)
})

我尝试了一些不同的格式(是否有其他Railsish的结构方式?),并且有很多不同的输入允许没有运气。我目前的尝试是

params.require(:people).each { |person| person.permit(:id, :first, :last) }

这不会崩溃,但实际上似乎也不允许任何事情。

我也尝试过使用people[][person][id]设置数据,以便可以

params.require(:people).permit(person: [:id, :first, :last]) 

但是那也不起作用。

1 个答案:

答案 0 :(得分:1)

[!] Flutter (Channel stable, v1.2.1, on Linux, locale en_US.utf8)
✗ Downloaded executables cannot execute on host.
  See https://github.com/flutter/flutter/issues/6207 for more information
  On Debian/Ubuntu/Mint: sudo apt-get install lib32stdc++6
  On Fedora: dnf install libstdc++.i686
  On Arch: pacman -S lib32-libstdc++5 (you need to enable multilib:
  https://wiki.archlinux.org/index.php/Official_repositories#multilib)

如果缺少密钥,第一行只是引发错误。 params.require(:people) params.permit(people: [:id, :first, :last]) 允许使用人员密钥和带有密钥.permit(people: [:id, :first, :last])的哈希数组。