Disable autopopulation of select fields with database values ββin rails 4 change form
I'm running Rails 4.1 and I have an edit form with some values ββin it, I don't mind other fields in the form, autopopulation from database values, Rails does it well. But I want to disable autopopulation of one field. Is it possible?
I'm doing the standard Rails form_for, everything works, I just don't want one of the fields to be autopopulated. I don't want the value to be empty or "" because it was overwriting the database.
Any suggestions?
edit.html.erb
<%= form_for @object do |f| %>
<% f.text_field :property %> #autopopulate from database
<% f.text_field :property2 %> # I do not want to autopopulate.
<% f.submit 'Submit %>
<% end %>
+3
source to share
1 answer
Clear attribute before rendering the form.
This is what it would look like in the view:
<% @object.property2.clear %> # set attribute to an empty string
<%= form_for @object do |f| %>
<% f.text_field :property %> #autopopulate from database
<% f.text_field :property2 %> # I do not want to autopopulate.
<% f.submit 'Submit %>
<% end %>
However, it's best to do this in a controller:
def edit
# ...
@object.property2.clear
end
Please note, if you submit this form with a blank field, it will save the blank line in the database, deleting everything that came before.
+1
source to share