# RAILS Views: entering NULL

**URL:** <https://www.ruby-forum.com/t/rails-views-entering-null/109574>\
**Category:** Ruby\
**Created:** [July 17, 2007, 12:22am UTC](https://www.ruby-forum.com/t/rails-views-entering-null/109574 "2007-07-17T00:22:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 17, 2007, 12:22am UTC](https://www.ruby-forum.com/t/rails-views-entering-null/109574/1 "2007-07-17T00:22:24Z")

</div>

I have looked around the net and can’t find the answer to this  
question. I have a date column that is allowed to be null. How do I  
allow the user to enter a null date in the form?

Old hand at Ruby, new to RAILS…

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 17, 2007, 1:13am UTC](https://www.ruby-forum.com/t/rails-views-entering-null/109574/2 "2007-07-17T01:13:30Z")

</div>

On Jul 16, 2007, at 17:10 , removed\_email\_address@domain.invalid wrote:

> I have looked around the net and can’t find the answer to this  
> question. I have a date column that is allowed to be null. How do I  
> allow the user to enter a null date in the form?
> 
> Old hand at Ruby, new to RAILS…

You might try the Ruby on Rails mailing list:

[http://groups.google.com/group/rubyonrails-talk](http://groups.google.com/group/rubyonrails-talk)

Michael G.  
grzm seespotcode net

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 17, 2007, 1:28am UTC](https://www.ruby-forum.com/t/rails-views-entering-null/109574/3 "2007-07-17T01:28:34Z")

</div>

removed\_email\_address@domain.invalid wrote:

> I have looked around the net and can’t find the answer to this  
> question. I have a date column that is allowed to be null. How do I  
> allow the user to enter a null date in the form?

AFAIK, there isn’t a preset, easy way to do it. Here’s my 2 ways:

1. If you’re using the date\_select helper, I would add a check\_box.  
Note that the check\_box field’s object\_name is not “your\_record\_object”  
If it was, the update\_attributes method would fail because there is no  
field called “your\_date\_field\_is\_null” in the record.

Like so: (Note: missing labels, etc)

```
<%=date_select 'your_record_object', 'your_date_field' %>

<%=check_box 'options', 'your_date_field_is_null' %>

```

Then in your controller, test for the checkbox and set the date field to  
nil if applicable.

Like so: (this could be prettier)

```
@record = YourRecordObject.find(params[:id])
if request.post?
  if @record.update_attributes(params[:your_record_object])
    if params[:options].has_key?('your_date_field_is_null') &&
         params[:options]['your_date_field_is_null'].eql? '1'
      @record.update_attribute(:your_date_field, nil)
    end
  end
end

```

1. If you’re using the text\_field helper, I would test the text\_field  
input for empty? (in the controller) and set it to nil if applicable

Like so: (this could be prettier)

```
@record = YourRecordObject.find(params[:id])

if request.post?
  if params[:your_record_object][:your_date_field].empty?
    params[:your_record_object][:your_date_field] = nil
  else
    params[:your_record_object][:your_date_field] =
        params[:your_record_object][:your_date_field].to_date
  end

  if @record.update_attributes(params[:your_record_object])
    flash[:notice] = "Your Record Object was updated successfully."
  end
end

```

–  
Travis W.

“Programming in Java is like dealing with your mom –  
it’s kind, forgiving, and gently chastising.  
Programming in C++ is like dealing with a disgruntled  
girlfriend – it’s cold, unforgiving, and doesn’t tell  
you what you’ve done wrong.”

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 17, 2007, 12:42pm UTC](https://www.ruby-forum.com/t/rails-views-entering-null/109574/4 "2007-07-17T12:42:44Z")

</div>

how about date\_select(_blah_, _blah_, :include\_blank =\> true)

removed\_email\_address@domain.invalid wrote:

> I have looked around the net and can’t find the answer to this  
> question. I have a date column that is allowed to be null. How do I  
> allow the user to enter a null date in the form?
> 
> Old hand at Ruby, new to RAILS…

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 18, 2007, 7:01pm UTC](https://www.ruby-forum.com/t/rails-views-entering-null/109574/5 "2007-07-18T19:01:31Z")

</div>

On Jul 17, 6:42 am, Matthew R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> how about date\_select(_blah_, _blah_, :include\_blank =\> true)

Ooh, nifty. Thanks!
