RAILS Views: entering NULL

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

Michael Glaesemann
grzm seespotcode net

···

On Jul 16, 2007, at 17:10 , markjreed@gmail.com 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...

markjreed@gmail.com 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

2. 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 Warlick

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

how about date_select(*blah*, *blah*, :include_blank => true)

markjreed@gmail.com 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...

--
Posted via http://www.ruby-forum.com/\.

Ooh, nifty. Thanks!

···

On Jul 17, 6:42 am, Matthew Rudy <matthewrudyjac...@gmail.com> wrote:

how about date_select(*blah*, *blah*, :include_blank => true)