简体   繁体   中英

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". 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM