6

Possible Duplicate:
Make blank params[] nil

Is there a way to make blank form inputs submit nil? Right now, I'm going through and in a before_save manually converting all "" into nil.

This really doesn't seem very DRY, and I feel like I must be missing something.

Community
  • 1
  • 1
William Jones
  • 18,089
  • 17
  • 63
  • 98
  • Why do you need to make them nil? The absence of them in the `INSERT`/`UPDATE` statement should make them `NULL` in the table row. Accessing a non-existant index from a hash (like `params`) would return nil. What else do you need? – ryeguy Jun 24 '10 at 21:15
  • With blank he means the empty string, "". This will in many cases save in the DB as the empty string. – nasmorn Jun 24 '10 at 21:18

2 Answers2

4

Check out the attribute_normalizer plugin.

Andrew Vit
  • 18,961
  • 6
  • 77
  • 84
0

There is no notion of "submitting nil" from a browser.

If field of name 'foo' is not submitted, then if you ask for params[:foo], it will return nil, which seems to be the behavior you desire.

Can you explain more what you want to do?

John Bachir
  • 22,495
  • 29
  • 154
  • 227