本文介绍了使用simple_form输入的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图输入默认值

正常:

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

但我需要f.input而不是f.input_field

but i need f.input not f.input_field

<%= f.input :quantity %>



  • im尝试使用标准html值进行尝试-但在验证失败后将1覆盖不完全-不需要

  • 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 %>
    


  • 如何解决?
    是否有类似f.input_field-:default的替代方法?
    还是有其他有价值的解决方案?

    推荐答案

    您可以尝试使用以下方法:

    You can try with something like this:

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

    这篇关于使用simple_form输入的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-14 02:55