简体   繁体   中英

Ruby on rails simple form text field value is overridden by default value

I am using a simple form like which has a text field with a default value. Once the user sets their desired value, the default value should be overridden by the user's desired value. But each time the user opens the form to edit it, they see the default value again and again:

<%= f.input :notes, input_html: {:value => @order_f.decorate.template_message, rows: 12} %>

请尝试设置占位符,而不是设置值:

<%= f.input :notes_to_deliverer, placeholder: @order_f.decorate.deliverer_template_message, input_html: {rows: 12} %>

If you don't need the placeholder then you can do it like this:

<%= f.input :notes_to_deliverer, input_html: {:value => object.notes_to_deliverer.present? ? object.notes_to_deliverer : @order_f.decorate.deliverer_template_message, rows: 12} %>

Here the object is for which you have created the form . Also please make sure that this value is saved in database on form submit. And if suppose this your default value to be stored what I mean is if user doesn't enters any value then you need this to be stored then it would be better to use default value at database end. You can set it in migration file.

Hope this helps.

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