Default value for input with simple_form

前端 未结 6 1961
小鲜肉
小鲜肉 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:34

    This is an old question...but, none of provided answers seem acceptable to me. The best way to do this is to set the value in the controllers new action.

     def new
       WizBang.new(quantity: 1)
    

    This will assign the objects quantity key to value 1 in the new action. The edit action should rely on the object's persisted value, or a params value if validation failed and the form reloaded. The other answers will force the quantity to 1 on edit, even if the user initially saved nil (if you allow nil). Not ok. I would not allow nil, but would include a 0 option in the quantity field.

    f.input :quantity, collection (0..100)
    

    much cleaner.

提交回复
热议问题