Saving dates

Hi having trouble saving dates in my form i have the following in the
view

<%= date_select(“details”, “open_date”) %>

and i have

@exhibition.open_date = params[:details][:open_date]

and then

@exhibition.save

All the other values form (not seen here) get saved but not the dates.
What am i doing wrong ?

Model and schema please…

askegg wrote:

Model and schema please…

== Schema Information

Schema version: 57

Table name: exhibitions

id :integer(11) not null, primary key

name :string(255)

subtitle :string(255)

short_description :string(255)

description :string(255)

curator :string(255)

artists :string(255)

url :string(255)

image :string(255) default(/exhibition/image/1_0.gif)

catalogue :string(255)

admission_price :text

open_date :date

close_date :date

user_id :integer(11)

gallery_id :integer(11)

class Exhibition < ActiveRecord::Base
has_one :advertisement
belongs_to :user
belongs_to :image
belongs_to :gallery
belongs_to :listing_status
has_and_belongs_to_many :organisations
has_and_belongs_to_many :artists
has_and_belongs_to_many :curators
has_and_belongs_to_many :festivals
has_and_belongs_to_many :tags
has_one :feature_exhibition
has_many :weeks

validates_presence_of :name, :short_description, :subtitle, :on =>
:create
#file_column :image

def self.find_exhibitions_currently_on
find(:all,
:conditions => “(open_date < now()) && (close_date > now())”,
:order => “close_date desc”
)
end

def self.find_exhibitions_closed
find(:all,
:conditions => “close_date < now()”,
:order => “close_date”)
end

def self.find_exhibitions_coming_up
find(:all,
:conditions => “open_date > now()”,
:order => “open_date”)
end
end

??? Damn. Looks fine to me.

Anything in the logs?

Drop <%= debug(params) %> into the view and see whats there.

askegg wrote:

??? Damn. Looks fine to me.

Anything in the logs?

Drop <%= debug(params) %> into the view and see whats there.

so its not something i am doing wrong in the code then… humm anyone
else with any ideas here?

On 12/14/06, Guest [email protected] wrote:

else with any ideas here?
Did you put

<%= debug(params) %> into the view like askegg said?

Guest wrote:

Daniel ----- wrote:

On 12/14/06, Guest [email protected] wrote:

else with any ideas here?
Did you put

<%= debug(params) %> into the view like askegg said?

what view?

The view of the page after the insert?
Of the view of the page where the user selects the date?

here is the dump of after…

— !map:HashWithIndifferentAccess
commit: Continue To Step 2 of 3…
details: !map:HashWithIndifferentAccess
name: This test
isfree: “no”
admission_price: “”
open_date(1i): “2006”
license: “1”
close_date(1i): “2006”
open_date(2i): “12”
close_date(2i): “12”
short_description: yes yes yes it is
open_date(3i): “14”
url: http://
close_date(3i): “14”
contributors: “”
subtitle: this is a test
description: “”
year: “2006”
copywho: “”
action: extradetails
controller: listing/add

Daniel ----- wrote:

On 12/14/06, Guest [email protected] wrote:

else with any ideas here?
Did you put

<%= debug(params) %> into the view like askegg said?

what view?

The view of the page after the insert?
Of the view of the page where the user selects the date?

I think Alan is right. Rails is expected a date, but you are passing
it an array.

Use date_select in your form instead of an array of values.
(ActionView::Helpers::DateHelper)

Stewart wrote:

@exhibition.open_date = params[:details][:open_date]

My intuition tells me that wont work because
params[:details][:open_date] is likely a hash, not a date object. I’m
not entirely sure how (or where) rails does the magic conversion on the
way in.

Note it’s only my intuituition, with nothing solid to back that up :slight_smile:

A.

askegg wrote:

I think Alan is right. Rails is expected a date, but you are passing
it an array.

Use date_select in your form instead of an array of values.
(ActionView::Helpers::DateHelper)

I think i already am…

here is my code anyway.

Date the exhibition opens:

<%= date_select("details", "open_date") %> *Required

Date the exhibition closes:

<%= date_select("details", "close_date") %> *Required

am i used date_select in the correct way?

askegg wrote:

That is the correct usage, but your controller is trying to pull the
date submitted apart and force it into the database.
Since the date fields are named with the database field name you can
just use:

Exhibition.create(params[:details])

instead of:

@exhibition.open_date = params[:details][:open_date]
@exhibition.save

If you want some error checking, you might want to try something like:

@exhibition = Exhibition.new(params[:details])
if @exhibition.save
flash[:notice] = “Exhibition created”
redirect_to :action => :index and return

Thanks askegg that was it. Rails needs you to send all the data at once
and not set it 1 by 1 like i was. I was not fully understanding how that
all worked. Now i do and i cant see myself making the same mistake
again.

Thanks for all your help with the posts.

That is the correct usage, but your controller is trying to pull the
date submitted apart and force it into the database.
Since the date fields are named with the database field name you can
just use:

Exhibition.create(params[:details])

instead of:

@exhibition.open_date = params[:details][:open_date]
@exhibition.save

If you want some error checking, you might want to try something like:

@exhibition = Exhibition.new(params[:details])
if @exhibition.save
flash[:notice] = “Exhibition created”
redirect_to :action => :index and return