Valor predeterminado para la entrada con formulario simple


Estoy tratando de hacer el valor predeterminado para la entrada

Funciona bien:

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

Pero necesito f. input no f. input_field

<%= f.input :quantity %> 


  • Lo intento con un valor html estándar, pero después de que la cantidad de validación no concluida se anula por 1-no deseado

    <%= f.input :quantity, input_html: {value: '1'} %>
    
  • Cuando elimino el valor y la validación no es concluyela cantidad completa se rellena - todo está bien

    <%= f.input :quantity %>
    

Cómo resolver esto ? ¿hay alguna alternativa como en f. input_field -: default ? o hay alguna otra solución con valor ?

Author: patie, 2013-09-26

3 answers

Puedes probar con algo como esto:

<%= f.input :quantity, input_html: {value: f.object.quantity || '1'} %>
 123
Author: Oleg Haidul,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-05-25 21:52:52

Puede usar la opción selected de simple_form: <%= f.input :quantity, selected: f.object.quantity || '1' %>

 1
Author: Stefan Lyew,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-15 20:20:39

Prueba esto:

= f.input : quantity, input_html: { value: (f.object.quantity.present?) ? f.object.quantity : '1' }
 1
Author: Kiry Meas,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-02 08:37:49