Ruby on Rails:提交表单中的数组

Wil*_*nes 66 forms arrays hash ruby-on-rails

我有一个具有Array属性的模型.从表单提交中填充该属性的正确方法是什么?

我知道有一个带有字段的表单输入,其名称包括括号,从输入创建一个哈希.我应该只是拿着它并在控制器中踩过它来按摩它成阵列吗?

让它不那么抽象的例子:

class Article
  serialize :links, Array
end
Run Code Online (Sandbox Code Playgroud)

links变量采用URL数组的形式,即 [["http://www.google.com"], ["http://stackoverflow.com"]]

当我在表单中使用类似下面的内容时,会创建一个哈希:

<%= hidden_field_tag "article[links][#{url}]", :track, :value => nil %>
Run Code Online (Sandbox Code Playgroud)

结果哈希看起来像这样:

"links" => {"http://www.google.com" => "", "http://stackoverflow.com" => ""}
Run Code Online (Sandbox Code Playgroud)

如果我没有在链接名称中包含url,则其他值会相互冲突:

<%= hidden_field_tag "article[links]", :track, :value => url %>
Run Code Online (Sandbox Code Playgroud)

结果如下: "links" => "http://stackoverflow.com"

Lar*_*y K 89

如果你的html表单的输入字段带有空方括号,那么它们将变成控制器中params内的数组.

# Eg multiple input fields all with the same name:
<input type="textbox" name="course[track_codes][]" ...>

# will become the Array 
   params["course"]["track_codes"]
# with an element for each of the input fields with the same name
Run Code Online (Sandbox Code Playgroud)

添加:

请注意,rails helper 设置为自动执行数组技巧.因此,您可能必须手动创建名称属性.此外,如果使用rails助手,复选框也有自己的问题,因为复选框助手会创建其他隐藏字段来处理未经检查的案例.


小智 51

= simple_form_for @article do |f|
  = f.input_field :name, multiple: true
  = f.input_field :name, multiple: true
  = f.submit
Run Code Online (Sandbox Code Playgroud)

  • 请添加解释此代码的作用.像这样答案并不是很有帮助. (12认同)
  • f.input:name,input_html:{Multiple:true}`可以用于输入 (3认同)
  • `f.input: name, multiple: true, value: ...` 也适用于 Rails 原生 `form_for @article do |f| ` (2认同)

eco*_*gic 41

TL; HTML版本的HTML []约定:

阵:

<input type="textbox" name="course[track_codes][]", value="a">
<input type="textbox" name="course[track_codes][]", value="b">
<input type="textbox" name="course[track_codes][]", value="c">
Run Code Online (Sandbox Code Playgroud)

Params收到:

{ course: { track_codes: ['a', 'b', 'c'] } }
Run Code Online (Sandbox Code Playgroud)

哈希

<input type="textbox" name="course[track_codes][x]", value="a">
<input type="textbox" name="course[track_codes][y]", value="b">
<input type="textbox" name="course[track_codes][z]", value="c">
Run Code Online (Sandbox Code Playgroud)

Params收到:

{ course: { track_codes: { x: 'a', y: 'b', z: 'c' } }
Run Code Online (Sandbox Code Playgroud)

  • @FilipeEsperandio`f.input"course [track_codes] [x]"` (2认同)

Ana*_*nco 8

我还发现,如果像这样传递你的输入助手,你将获得一系列课程,每个课程都有自己的属性.

# Eg multiple input fields all with the same name:
<input type="textbox" name="course[][track_codes]" ...>

# will become the Array 
   params["course"]

# where you can get the values of all your attributes like this:
   params["course"].each do |course|
       course["track_codes"]
   end    
Run Code Online (Sandbox Code Playgroud)


Pet*_*ich 7

我只是使用jquery taginput设置了一个解决方案:

http://xoxco.com/projects/code/tagsinput/

我写了一个自定义的simple_form扩展

# for use with: http://xoxco.com/projects/code/tagsinput/

class TagInput < SimpleForm::Inputs::Base

  def input
    @builder.text_field(attribute_name, input_html_options.merge(value: object.value.join(',')))
  end

end
Run Code Online (Sandbox Code Playgroud)

一个coffeescrpt片段:

$('input.tag').tagsInput()
Run Code Online (Sandbox Code Playgroud)

并调整我的控制器,遗憾的是必须具体:

@user = User.find(params[:id])
attrs = params[:user]

if @user.some_field.is_a? Array
  attrs[:some_field] = attrs[:some_field].split(',')
end
Run Code Online (Sandbox Code Playgroud)