[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

RAILS Views: entering NULL

Zeekar

7/16/2007 10:06:00 PM

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

4 Answers

Michael Glaesemann

7/16/2007 10:36:00 PM

0


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

You might try the Ruby on Rails mailing list:

http://groups.google.com/group/rubyon...

Michael Glaesemann
grzm seespotcode net



Travis D Warlick Jr

7/16/2007 11:02:00 PM

0

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

Matthew Rudy Jacobs

7/17/2007 10:43:00 AM

0

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

Zeekar

7/18/2007 4:56:00 PM

0

On Jul 17, 6:42 am, Matthew Rudy <matthewrudyjac...@gmail.com> wrote:
> how about date_select(*blah*, *blah*, :include_blank => true)

Ooh, nifty. Thanks!