简体   繁体   English

Rails 强大的参数,接受多种数据类型

[英]Rails Strong Params, Accept Multiple Data Types

Is there any way to allow Rails strong params to permit different data types?有什么方法可以让 Rails strong params 允许不同的数据类型吗? For example, I'm using react-bootstrap-typeahead and redux-form to create a form that allows users to select from provided values or create their own value.例如,我正在使用 react-bootstrap-typeahead 和 redux-form 创建一个表单,允许用户从提供的值 select 或创建他们自己的值。 The provided values come from the database and are handed to the controller as an object with name and id whereas the user created values are passed as a string.提供的值来自数据库,并作为带有名称和 ID 的 object 传递给 controller,而用户创建的值作为字符串传递。

Permitting both a String and Array/Object in Rails 4+在 Rails 4+ 中允许字符串和数组/对象

TL;DR: Simply list parameter multiple times to support different types. TL;DR:简单地多次列出参数以支持不同的类型。

  • String or Array: params.require(:my_model).permit(:my_attr, my_attr: [])字符串或数组: params.require(:my_model).permit(:my_attr, my_attr: [])
  • String or Object: params.require(:my_model).permit(:my_attr, my_attr: [:key1, :key2])字符串或 Object: params.require(:my_model).permit(:my_attr, my_attr: [:key1, :key2])

Full Example完整示例

Suppose we have a model MyModel which has an attribute my_attr .假设我们有一个 model MyModel ,它有一个属性my_attr

my_attr can be a string or array: my_attr可以是字符串或数组:

# example requests: 
#   - {"my_model": {"my_attr": "hello" }
#   - {"my_model": {"my_attr": ["hello", "world"] }
params.require(:my_model).permit(:my_attr, my_attr: []) 

my_attr can be a string or object: my_attr可以是字符串或 object:

# my_attr can be a string or an object with keys key1, key2
# example requests: 
#   - {"my_model": {"my_attr": "hello" }
#   - {"my_model": {"my_attr": { "key1": "val1", "key2": "val2" }
params.require(:my_model).permit(:my_attr, my_attr: [:key1, :key2])

The first argument to permit allows my_attr to be a string, the second allows it to be an array or object ( see documentation for Rails parameters ). permit的第一个参数允许my_attr是一个字符串,第二个参数允许它是一个数组或 object( 请参阅 Rails 参数文档)。

Testing In Rails Console在 Rails 控制台中测试

Copy/Paste the following code to experiment with params directly:复制/粘贴以下代码以直接试验参数:

# Testing with String
params = ActionController::Parameters.new(my_model: { my_attr: "My String" })
permitted = params.require(:my_model).permit(:my_attr, my_attr: [])
permitted.permitted?      # => true

# Testing with Array
params = ActionController::Parameters.new(my_model: { my_attr: ["Hello", "World"] })
permitted = params.require(:my_model).permit(:my_attr, my_attr: [])
permitted.permitted?      # => true

# Testing with Object
params = ActionController::Parameters.new(my_model: { my_attr: {key1: 'attr1', key2: 'attr2' }})
permitted = params.require(:my_model).permit(:my_attr, my_attr: [:key1, :key2])
permitted.permitted?      # => true

I recently did something like我最近做了类似的事情

params.require(:note).permit(
  fields: [:value, value: []]
)

which allowed me to submit string and array params.这允许我提交字符串和数组参数。

Sorry I don't have the docs to back me up on that, I'm just learning ruby myself.抱歉,我没有文档来支持我,我只是自己学习 ruby。 Hope this helps.希望这可以帮助。

A form's submitted values will be stored in params[:object] regardless.无论如何,表单提交的值都将存储在 params[:object] 中。 I'm still not sure what you're asking - can you post your create controller actions if that's what being called on submit?我仍然不确定你在问什么 - 如果提交时调用了 create controller 操作,你能发布吗?

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

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