简体   繁体   中英

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.

HTML does not have variables. 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. In your controller you could assign the value of a input called field like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM