Default value for input with simple_form

前端 未结 6 1940
小鲜肉
小鲜肉 2021-02-03 16:53

im trying to do default value for input

works ok:

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

but i need f.i

6条回答
  •  情书的邮戳
    2021-02-03 17:42

    Now sure how a duplicate Question's answers get referenced, but I am sharing an Answer I just left on a question I flagged as a duplicate.

    Here is a summary for this question:

     # simple_form input
     f.input :quantity, input_html: {value: f.object.quantity || '1'}
    

    can become:

     # simple_form input
     = f.input :quantity, input_html: { value: f.object.quantity_with_default }
    
     # Model with date_start attribute
     class Obj
       def quantity_with_default
         # based on your model, you may need this instead: try(:quantity) || '1' 
         quantity || '1' 
       end
     end
    

    This leaves the management of the data and its defaults in the controller instead of sprinkled throughout the HTML

提交回复
热议问题