通过隐藏字段将数组传递到 Rails

发布于 2024-12-01 04:47:03 字数 565 浏览 4 评论 0原文

我的表单中有一个像这样的hidden_​​tag,

 <%= f.hidden_field :loc , {:multiple => true}  %>

它呈现为

 <input id="business_loc" multiple="multiple" name="business[loc][]" type="hidden" style="color: rgb(175, 175, 175); " value="">

当前正在将business_loc值设置为逗号分隔的字符串,希望rails在提交表单时能够识别。但这是我在服务器端获得的值,

      "loc"=>["80.22167450000006,13.0454044"] 

而不是

      "loc"=>[80.22167450000006,13.0454044] 

我如何在隐藏字段中设置正确的值,以便rails可以正确理解它。

i have a hidden_tag like this in my form

 <%= f.hidden_field :loc , {:multiple => true}  %>

which renders to

 <input id="business_loc" multiple="multiple" name="business[loc][]" type="hidden" style="color: rgb(175, 175, 175); " value="">

currently am setting the business_loc value as a comma seperated string hoping rails would recognize when submit the form. But this is the value i got on the server side

      "loc"=>["80.22167450000006,13.0454044"] 

instead

      "loc"=>[80.22167450000006,13.0454044] 

how do i set the correct value in hidden field, so rails can understand it correctly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

〆凄凉。 2024-12-08 04:47:03

您需要使用多个隐藏字段,每个隐藏字段对应值数组的每个元素。

例如:

<input id="business_loc" multiple="multiple" name="business[loc][]" type="hidden" style="color: rgb(175, 175, 175); " value="80.22167450000006">
<input id="business_loc" multiple="multiple" name="business[loc][]" type="hidden" style="color: rgb(175, 175, 175); " value="13.0454044">

...如果您需要代码来使用 JS 动态添加这些内容,这里有一个 jQuery 示例:

var field = $('<input id="business_loc" multiple="multiple" name="business[loc][]" type="hidden" style="color: rgb(175, 175, 175); " value="13.0454044">');
var form = $('#your-form-id');
form.append(field);

You need to use multiple hidden fields, one for each element of the array of values.

For example:

<input id="business_loc" multiple="multiple" name="business[loc][]" type="hidden" style="color: rgb(175, 175, 175); " value="80.22167450000006">
<input id="business_loc" multiple="multiple" name="business[loc][]" type="hidden" style="color: rgb(175, 175, 175); " value="13.0454044">

...if you need code to dynamically add these with JS, here's a jQuery example:

var field = $('<input id="business_loc" multiple="multiple" name="business[loc][]" type="hidden" style="color: rgb(175, 175, 175); " value="13.0454044">');
var form = $('#your-form-id');
form.append(field);
永不分离 2024-12-08 04:47:03

我发现 text_area 可以使事情正常工作,而无需添加一堆隐藏表单。只需将文本区域的值设置为类似于 [1,31,51,61] 的值,它就应该可以工作,假设您的模型中有 serialize :var

I've found text_area's to make things work without having to add a bunch of hidden forms. Just set the value of the text area to something that looks like [1,31,51,61] and it should work, assuming in your model you have serialize :var

无所的.畏惧 2024-12-08 04:47:03

我最近也遇到了同样的问题。我的解决方案是通过简单地在逗号处分割数组来在服务器端处理它。就我而言,它看起来像这样:

  # thing_that_has_many_objects.rb     <-- showing custom setter method from the model because my example involves using a virtual attribute
  # params[object_ids] = ["1,2,3,4,5"] <-- from the form - note the format of array with only one element

  def objects=(object_ids)       
    split_array = object_ids[0].split(',') 
    split_array.each do |id|
      self.objects.build(object_id: id)
    end
  end

I had this same problem recently. My solution was to handle it on the server side by simply splitting the array at the comma. In my case it looks like this:

  # thing_that_has_many_objects.rb     <-- showing custom setter method from the model because my example involves using a virtual attribute
  # params[object_ids] = ["1,2,3,4,5"] <-- from the form - note the format of array with only one element

  def objects=(object_ids)       
    split_array = object_ids[0].split(',') 
    split_array.each do |id|
      self.objects.build(object_id: id)
    end
  end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文