[英]Rails Simple Form Default Value if Submitted Nil
I need to overwrite any blank field submissions in my simple form with the text value "N/A". 我需要用文本值“ N / A”以简单的形式覆盖所有空白字段提交的内容。 I've found how to set a default value beforehand, but I would like the field left blank for users to fill out and submit, and only changed if they leave it blank. 我已经找到了如何预先设置默认值的方法,但是我希望该字段留空以供用户填写和提交,并且只有在用户将其保留为空的情况下才进行更改。 Is this possible? 这可能吗? Thanks! 谢谢!
<%= f.input :survey_year, :input_html => { :value => 'N/A'} %>
Should do the trick. 应该做到的。 See the flipflops.org link in my comment above for alternative approaches. 有关其他方法,请参见我上面的评论中的flipflops.org链接。
Try to the following 尝试以下
before_save :default_values
def default_values
self.name ||= 'N/A' #=> note self.name = 'N/A' if self.name.nil?
end
When a user submits a form with blank/nil
name then it will submit to "N/A"
otherwise none 用户提交名称为blank/nil
的表单时,它将提交为"N/A"
否则为"N/A"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.