Ruby-on-rails rails - hidden_field 和 hidden_field_tag 到底是做什么的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7733085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
rails - what exactly does hidden_field and hidden_field_tag do?
提问by noob
I read through the techy definition of hidden_fields, but am not sure what it really does. My understanding is that it allows you to pass in an attribute for certain parameters. For example, if you have a rich join model, you can use the hidden_fieldto assign the user_id to the join model attribute for user. Is that correct?
我通读了 的技术定义hidden_fields,但不确定它到底是做什么的。我的理解是它允许您为某些参数传入一个属性。例如,如果您有一个丰富的联接模型,则可以使用hidden_field将 user_id 分配给用户的联接模型属性。那是对的吗?
If so, would it be better to do it in the form or the controller?
如果是这样,在表单或控制器中进行会更好吗?
回答by Wizard of Ogz
Both of those methods are helpers to create an HTML input tag of type "hidden", and yes, those are used to add parameters to a request (typically a form POST). Really the parameter can be any piece of information you want to send along with a request. Be careful, though, as hidden fields are easily tampered with.
这两种方法都是创建“隐藏”类型的 HTML 输入标签的助手,是的,它们用于向请求(通常是表单 POST)添加参数。实际上,参数可以是您想与请求一起发送的任何信息。但是要小心,因为隐藏字段很容易被篡改。
Here's an example that will send a user id in a hidden field
这是一个将在隐藏字段中发送用户 ID 的示例
# Form
<%= form_tag foo_path do %>
<%= hidden_field_tag "user_id", @user.id %>
....
<%= submit_tag "Click Me" %>
<% end %>
# Controller
def foo
# params[:user_id] is set with the value from the hidden field
# Do useful stuff with the POST data
end
While you can pass things such as user_id's like this, I find that the need for it is rare. If a user_id is always required for a given situation you might consider using nested routes http://guides.rubyonrails.org/routing.html#nested-resources.
虽然您可以像这样传递诸如 user_id 之类的东西,但我发现很少需要它。如果在给定情况下始终需要 user_id,您可以考虑使用嵌套路由http://guides.rubyonrails.org/routing.html#nested-resources。
回答by Chad Moran
It would generate a hidden type of input field...
它会生成一个隐藏类型的输入字段......
<input type="hidden" />
<input type="hidden" />
This is a way to store information that you want submitted with the form without having to have a visible field.
这是一种存储您希望随表单提交的信息而无需具有可见字段的方法。
From the documentation:
从文档:
hidden_field_tag 'tags_list'generates...<input id="tags_list" name="tags_list" type="hidden" />
hidden_field_tag 'tags_list'产生...<input id="tags_list" name="tags_list" type="hidden" />
hidden_field_tagis meant to be used without a model whereas hidden_fieldis meant to be used in conjunction with a form_forcall and a model.
hidden_field_tag旨在在没有模型的情况下使用,而hidden_field旨在与form_for调用和模型结合使用。
hidden_field(:signup, :pass_confirm)generates...<input type="hidden" id="signup_pass_confirm" name="signup[pass_confirm]" value="#{@signup.pass_confirm}" />
hidden_field(:signup, :pass_confirm)产生...<input type="hidden" id="signup_pass_confirm" name="signup[pass_confirm]" value="#{@signup.pass_confirm}" />

