假设我有这个简单的搜索表单:
<%= form_tag invoices_path, :method => 'get' do %>
<%= text_field_tag :number %>
<%= text_field_tag :date %>
<%= text_field_tag :total %>
...
<%= submit_tag %>
<% end %>
这是我的 Controller :
class InvoicesController < ApplicationController
def index
@invoices = current_user.invoices.search(params)
end
...
end
如何从 params
哈希中省略某些参数,例如总计
?
我尝试过诸如params. except(:total)
之类的方法,但这并没有改变任何东西。
我不希望将total
发送到search
函数,也不希望它出现在URL中。
如何做到这一点?
感谢您的帮助。
最佳答案
如果表单中包含输入内容,除非您采取特殊操作,否则它将与表单一起提交。当您进入服务器上的 Rails 代码时,已经太晚了。
一种方法是在提交表单之前立即禁用该元素:
在您的输入中添加一个 never-submit
类:
<%= text_field_tag :total, '', :class => 'never-submit' %>
然后在提交表单之前使用 jQuery 禁用该类的任何元素:
$(document).ready(function() {
$('form').submit(function() {
$('.never-submit').prop('disabled', true);
return true;
});
});
您可能还需要考虑将该字段设置为输入
之外的其他内容。输入旨在允许用户向您的服务器提交数据。如果这不是该字段的意图,那么诸如 span
(样式看起来像 input
)之类的东西可能更合适。
关于ruby-on-rails - 通过 GET 发送表单时如何忽略某些参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25088868/