将参数数组从表单转换为字符串
我有一个带有复选框的表单:
-form_tag filter_path(@page.permalink), :method => 'get' do |f|
-ftype.producers.each do |producer|
=check_box_tag "producers[]", producer.id, false
=label_tag producer.title
%br
=submit_tag 'Сортувати', :name => nil
当我发送请求时,它会发送带有生产者数组的哈希参数。然后链接看起来像这样:
'/pages/:page_id/filter?producers[]=4&producers[]=5'
我想让它看起来像这样:
'/pages/:pages_id/filter?producers=4,5'
请帮忙
I have a form with a checkboxes:
-form_tag filter_path(@page.permalink), :method => 'get' do |f|
-ftype.producers.each do |producer|
=check_box_tag "producers[]", producer.id, false
=label_tag producer.title
%br
=submit_tag 'Сортувати', :name => nil
When I send a request, it sends a hash params with an array of producers.Link then looks like that:
'/pages/:page_id/filter?producers[]=4&producers[]=5'
And I want to make it look that:
'/pages/:pages_id/filter?producers=4,5'
Please help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该不是问题,因为
? Producers[]=4& Producers[]=5
将被框架转换为params[: Producers]
数组,其中包含值[4, 5]
。因此,您已经有了一个数组,甚至不需要解析任何内容。
但如果您确实想在一个参数中提交两个输入值,则必须使用一些 JavaScript。默认情况下,如果您在 html 表单中有两个同名的输入,则将提交两个独立的值(如您提供的示例 url 中所示)。
所以,这不是 Rails 问题,而是 html 和 javascript 问题。
It shouldn't be a problem, since
?producers[]=4&producers[]=5
will be converted by the framework intoparams[:producers]
array with value[4, 5]
.Thus, you already have an array and you don't even have to parse anything.
But if your really want to submit two input values in one parameter, you'd have to employ some javascript. By default, if you have in html form two inputs with the same name, two independent values will be submitted (like in sample url you provided).
So, it's not a Rails question, it's html and javascript question.