通过隐藏字段将数组传递到rails [英] Passing array via hidden fields to rails

查看:101
本文介绍了通过隐藏字段将数组传递到rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表单中有一个类似的hidden_​​tag

i have a hidden_tag like this in my form

 <%= 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值设置为逗号分隔的字符串,希望在提交表单时可以识别.但这是我在服务器端获得的价值

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"] 

代替

      "loc"=>[80.22167450000006,13.0454044] 

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

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

推荐答案

您需要使用多个隐藏字段,每个隐藏字段用于值数组的每个元素.

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

例如:

<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示例:

...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);

这篇关于通过隐藏字段将数组传递到rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆