Rails 表单:如何附加请求参数?
我有一个列表页面,我通过带有 get params 的链接过滤项目(我可以选择许多链接,因此查询将类似于 "?param1=value1¶m2=value2"
)。但我还必须通过文本字段过滤它,所以我制作了一个表单:
<form>
<%= text_field_tag :zip, params[:zip] %>
<%= submit_tag 'OK', :name => nil %>
</form>
但是当我提交它时,文本字段参数会替换现有的查询参数。那么,如何使文本字段值添加到查询中,而不是替换它呢?
I got a list page and I filter items via links with get params (I can choose many links so query would be like "?param1=value1¶m2=value2"
). But also I have to filter it by text field, so I made a form:
<form>
<%= text_field_tag :zip, params[:zip] %>
<%= submit_tag 'OK', :name => nil %>
</form>
But when I submit it, text field param replaces existing query params. So, how to make text field value add to query, not to replace it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为我刚刚在 Rails 4 中处理这个问题,所以我想分享一下我的解决方案。
我的页面加载了 sport_id 参数,当用户指定排序顺序时,我希望它提交 page.url/event?sport_id=1&sortby=viewers 的 GET 请求,但它不会保留 sport_id 参数,直到我在表单中添加了一个隐藏字段标记,如下所示:
如果该参数不在原始请求中,则此解决方案确实会提交一个空的 sport_id 参数,但是通过将隐藏字段封装在 <% if 中可以轻松防止这种情况params[:sport_id].present? %>健康)状况。
Since I was just dealing with this problem in Rails 4 I thought I'd share my solution.
My page gets loaded with a sport_id parameter, and when the user specifies a sort-order I wanted it to submit a GET request for page.url/event?sport_id=1&sortby=viewers but it wouldn't preserve the sport_id parameter until I added a hidden field tag in the form like so:
This solution does submit an empty sport_id parameter if that parameter was not in the original request, but that is easily prevented by encapsulating the hidden field in an <% if params[:sport_id].present? %> condition.
使用
hidden_field_tag
。在表单内部,只需为现有
GET
参数设置hidden_field_tags
即可,如下所示:这将确保现有参数持续存在。
Use
hidden_field_tag
.Inside of your form, just set
hidden_field_tags
for the existingGET
params, like so:This will ensure that your existing params persist.
铁轨3?
Rails 3?