0

From my understanding, empty form fields saving to database usually defaults to nil (which is what I wanted). But two of my optional form fields are saving as empty ("") instead of nil.

I'm not sure why this is, I've looked at: Understanding Rails validation: what does allow_blank do? and Save blank value as nil in database thinking it may be because I am using allow_blank: true.

My group.rb

# meetup_urlname must be unique
validates :meetup_urlname, uniqueness: { case_sensitive: false, message: "this is already being used by a different group" }, allow_blank: true


# only validate if meetup_apikey or if meetup_urlname is non-empty
validate :url_name_valid?, :if => :meetup_urlname?
validate :api_key_valid?, :if => :meetup_apikey?

_Form.html.erb:

<%= bootstrap_form_for(@group, :html => { :multipart => true }, layout: :horizontal, label_col: "col-sm-3", control_col: "col-sm-9") do |f| %>
... 
  <h5>Import Events from Meetup</h5>

  <div class="field">
    <%= f.text_field :meetup_urlname, label: "Meetup Group's URL", type: "text", placeholder: "Enter your Meetup group's url to retrieve your events", prepend: 'http://www.meetup.com/'  %>
  </div>

  <div class="field">
    <%= f.text_field :meetup_apikey, label: 'Meetup API Key', placeholder: 'Enter your Meetup API key  (Required for advanced meetup functionality)' %>
  </div>

<%= f.hidden_field :status %>
<%= f.hidden_field :audit_status %>
<%= f.hidden_field :sref %>

<div class="col-sm-10" style="padding-right: 26px;">
  <div class="actions pull-right">
    <%= f.submit 'Update Group', class: "action-btn btn-rounded btn-large" %>
  </div>
</div>

<% end %>

Is there a way to have the optional form fields save as nil instead of empty by default? Currently have a hack in where if the form field returned empty..it would overwrite as nil.

Any feedback or insight would help. Thanks!

Community
  • 1
  • 1
teresa
  • 356
  • 5
  • 21
  • Why are you allowing blank? Normally that's disabled to avoid this very problem. – tadman Nov 16 '16 at 23:03
  • @tadman, sorry I guess that was one way I thought to add the optional form fields but still have validation if something was entered in the form fields. I didn't know if I could use something else to allow the field to be optional.. is it better to pass in a if statement to the `validates :meetup_urlname` ? – teresa Nov 16 '16 at 23:18
  • You can always push that `if` logic inside the validator itself if you want. The `if` clause is usually a better fit for those that don't have an associated method like `validates ..., presence: true, if: ...` – tadman Nov 16 '16 at 23:25

2 Answers2

0

The strip_attributes gem can take care of this for you.

https://github.com/rmm5t/strip_attributes

David Aldridge
  • 51,479
  • 8
  • 68
  • 96
  • This gem seem to work for me, but it also does other things which I can't be sure if it'll break my app in other places. Is there another way to write in the optional form field which only validates when a condition is met? Thanks. – teresa Nov 18 '16 at 17:36
0

Could you post more context, please?

With what we've got here, my ideas are:

  1. "" comes from somewhere in the view (please paste it)
  2. Perhaps you're calling somewhere to_s on the :meetup_urlname? nil.to_s will return "" in ruby.
medik
  • 1,174
  • 12
  • 17
  • That's interesting.. I think this could be the reason why it saves as empty. I have posted my `_form.html.erb` from view but I don't see a problem with it. Have not called `to_s` on `meetup_urlname` or `meetup_apikey` anywhere. – teresa Nov 17 '16 at 00:42