【问题标题】:Rails how to render array field in template use form_builder?Rails如何在模板中使用form_builder渲染数组字段?
【发布时间】:2012-02-13 04:17:53
【问题描述】:

我正在使用 mongoid,并将地理位置信息保存在一个数组字段中。

在我的模型中,我有:

field :location, :type => Array

在我看来,我有:

<%= f.hidden_field :location %>

这给出了结果:

<input id="foo_location" name="foo[location]" type="hidden">

我想要的如下:

<input id="foo_location_1" name="foo[location][]" type="hidden">
<input id="foo_location_2" name="foo[location][]" type="hidden">

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3


    【解决方案1】:

    为了将location 参数作为数组传回,您必须明确定义这些字段的输入名称:

    <% @foo.location.each do |loc| %>
      <%= f.hidden_field :location, :name => "foo[location][]", :value => loc %>
    <% end %>
    

    【讨论】:

    • 表单是新对象,所以@foo.location为空。
    【解决方案2】:

    我通过设置:multiple =&gt; true 找到了解决方案,这将给出正确的结果。

    <%= f.hidden_field :location, :multiple => true, :id => "foo_location_1" %>
    <%= f.hidden_field :location, :multiple => true, :id => "foo_location_2" %>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-11
      • 1970-01-01
      • 2011-04-28
      • 2011-08-28
      • 2014-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多