Ruby-on-rails 使用 simple_form 输入的默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19029129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Default value for input with simple_form
提问by patie
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 而不是 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 %>
我用标准的 html 值尝试它 - 但在不成功的验证数量被 1 覆盖后 - 不需要
<%= f.input :quantity, input_html: {value: '1'} %>当我删除值并且验证不成功时填充数量 - 一切正常
<%= 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 ?
如何解决这个问题?有没有像 f.input_field - :default 那样的替代方法?或者还有其他有价值的解决方案吗?
回答by Oleg Haidul
You can try with something like this:
你可以尝试这样的事情:
<%= f.input :quantity, input_html: {value: f.object.quantity || '1'} %>
回答by Stefan Lyew
You can use the selectedoption of simple_form:
<%= f.input :quantity, selected: f.object.quantity || '1' %>
您可以使用selectedsimple_form 选项:
<%= f.input :quantity, selected: f.object.quantity || '1' %>
回答by Kiry Meas
try this:
尝试这个:
= f.input : quantity, input_html: { value: (f.object.quantity.present?) ? f.object.quantity : '1' }
回答by hellion
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.
这将在新操作中将对象数量键分配给值 1。如果验证失败并重新加载表单,则编辑操作应依赖于对象的持久值或参数值。其他答案将在编辑时强制数量为 1,即使用户最初保存为 nil(如果您允许 nil)。不好。我不会允许 nil,但会在数量字段中包含一个 0 选项。
f.input :quantity, collection (0..100)
much cleaner.
干净多了。
回答by emptywalls
You can do
你可以做
<%= f.input :quantity, value: f.object.quantity || '1' %>
Nowadays, leaving off the input_htmlkey.
如今,撇开input_html关键。
回答by SMAG
Now sure how a duplicate Question's answers get referenced, but I am sharing an AnswerI 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
这将数据及其默认值的管理留在控制器中,而不是散布在整个 HTML 中

