Default value for input with simple_form

im trying to do default value for input

works ok:

<%= f.input_field :quantity, default: '1' %> 

but i need f.input not f.input_field

<%= f.input :quantity %> 


  • im trying it with standard html value - but after unsucessfull validation quantity is overriden by 1 - undesired

    <%= f.input :quantity, input_html: {value: '1'} %>
  • when i remove value and validation is unsucessfull quantity is populated - everything is ok

    <%= f.input :quantity %>

how to solve this ? is there any alternative like in f.input_field - :default ? or there is any other solution with value ?


Solution 1:

You can try with something like this:

<%= f.input :quantity, input_html: {value: f.object.quantity || '1'} %>

Solution 2:

You can use the selected option of simple_form: <%= f.input :quantity, selected: f.object.quantity || '1' %>

Solution 3:

try this:

= f.input : quantity, input_html: { value: (f.object.quantity.present?) ? f.object.quantity : '1' }