[英]How to assign data to a variable on a form? [Ruby on rails]
I wanted to assign a value from a form, to a variable, so I can work with that value before I save it. 我想将表单中的值分配给变量,因此在保存之前可以使用该值。 Also how I add a t.text_field that doesn't require a variable, wont be saved, wont do anything, i just wanted to be there.
还有我如何添加不需要变量的t.text_field,将不会被保存,不会做任何事情,我只是想在那里。
HTML does not have variables. HTML没有变量。 I'm assuming you want to assign the value of a form input to a variable in your controller?
我假设您想将表单输入的值分配给控制器中的变量? That is what the params hash is for.
那就是params hash的目的。 In your controller you could assign the value of a input called field like this
在您的控制器中,您可以像这样分配名为field的输入的值
myField = params[:thing][:field]
You can always add an input to a form and ignore it in your controller. 您始终可以将输入添加到表单,并在控制器中将其忽略。 Open the form with something like this
像这样打开表格
<%= form_for @thing do |f| %>
Then you can add a random input that's not part of your thing model like 然后,您可以添加一个不属于您的事物模型的随机输入,例如
<%= text_field_tag :randomInput %>
You can read up on form helpers here http://guides.rubyonrails.org/form_helpers.html 您可以在http://guides.rubyonrails.org/form_helpers.html上阅读表单助手。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.