Creating Dates from a form

58 views
Skip to first unread message

DAZ

unread,
Oct 7, 2012, 6:48:46 AM10/7/12
to datam...@googlegroups.com
I have a Song class that looks like this:

  class Song
    include DataMapper::Resource
    property :id, Serial
    property :title, String
    property :lyrics, Text
    property :length, Integer
    property :released_on, Date
  end

Here's my form for creating it:

  <form action="/songs" method="POST">
    <input id="title" name="song[title]" type="text" />
    <input id="length" name="song[length]" type="number" />
    <input id="day" max="31" min="1" name="day" type="number" />
    <input id="month" max="12" min="1" name="month" type="number" />
    <input id="year" max="1998" min="1940" name="year" type="number" />
    <textarea id="lyrics" name="song[lyrics]"></textarea>
    <input type="submit" value="Save Song" />
  </form>

I want to be able to create a new song resource by using

  song = Song.create(params[:song])

The problem I'm having is with the released_on property, since it needs to be a Date object.

At the moment, I am doing this:

  date = Date.new(params[:year].to_i,params[:month].to_i,params[:day].to_i)
  song = Song.create(params[:song].merge(released_on: date))

Does anybody know any nicer ways of entering a date into a form and being able to simply call Song.create(params[:song]) without having to manipulate the form parameters first?

cheers,

DAZ

Ben Lovell

unread,
Oct 7, 2012, 9:04:36 AM10/7/12
to datam...@googlegroups.com
I think this question would be better served in whichever list pertains to your chosen web framework (Sinatra I suspect?) but the simple answer is to use one of the many available date pickers. 


Regards,
Ben

Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "DataMapper" group.
To view this discussion on the web visit https://groups.google.com/d/msg/datamapper/-/OOhRt2vQKNEJ.
To post to this group, send email to datam...@googlegroups.com.
To unsubscribe from this group, send email to datamapper+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/datamapper?hl=en.

DAZ

unread,
Oct 9, 2012, 2:48:43 PM10/9/12
to datam...@googlegroups.com
Thanks for the reply Ben.

I think my question is more to do with DataMapper, because DataMapper requires the format to be a Date or DateTime object. My question is how I change what is effectively a string in the params hash into a date object in a neat way. Even if I used a date picker, I would assume that it sends some sort of string to the server rather than an actual Date object?

Hope that clarifies my question - is there a neat way of converting dates to Date objects or is there just some donkey work that needs doing before creating the resource?

cheers,

DAZ

Ben Lovell

unread,
Oct 9, 2012, 3:14:18 PM10/9/12
to datam...@googlegroups.com
On 9 October 2012 19:48, DAZ <daz...@gmail.com> wrote:
Thanks for the reply Ben.

I think my question is more to do with DataMapper, because DataMapper requires the format to be a Date or DateTime object. My question is how I change what is effectively a string in the params hash into a date object in a neat way. Even if I used a date picker, I would assume that it sends some sort of string to the server rather than an actual Date object?


In your example form you have three separate fields which you need to parse into a valid Date. You cannot expect DM to do this on your behalf nor should you. DM resources can parse date strings as properties of course, but you need to massage your params to get the date into an acceptable
 format. My point was that this would be a responsibility of your web framework or front-end not DM.

A similar example of this is the DateHelper.date_select in rails which does some hocus pocus with param suffixes and Date::civil to get the values from the three options into a proper date that AR can understand.

Cheers,
Ben
 
To view this discussion on the web visit https://groups.google.com/d/msg/datamapper/-/6zgiTuxv7zwJ.

DAZ

unread,
Oct 9, 2012, 3:58:24 PM10/9/12
to datam...@googlegroups.com
Hi Ben,

Thanks for the reply. This is what I thought was the case, but I just wondered if there were any nice DM methods that I didn't know about that parsed strings in a certain format, ready to insert into the database as a Date object.

I've found that the jQuery datepicker gives the date in this format 10/09/2012, so I have added the following method to the Song class to change it into a Date object:

  def released_on=date
    dates = date.split('/').map{ |s| s.to_i }
    super(Date.new(dates[2],dates[0],dates[1]))
  end

Not sure if that's the best way of doing it, but it certainly works!

thanks for the help and feedback!

DAZ

Ben Lovell

unread,
Oct 9, 2012, 4:16:58 PM10/9/12
to datam...@googlegroups.com
Hi Daz,

On 9 October 2012 20:58, DAZ <daz...@gmail.com> wrote:
Hi Ben,

Thanks for the reply. This is what I thought was the case, but I just wondered if there were any nice DM methods that I didn't know about that parsed strings in a certain format, ready to insert into the database as a Date object.

I've found that the jQuery datepicker gives the date in this format 10/09/2012, so I have added the following method to the Song class to change it into a Date object:

  def released_on=date
    dates = date.split('/').map{ |s| s.to_i }
    super(Date.new(dates[2],dates[0],dates[1]))
  end

I'd suggest: 

def released_on=date
  super(Date.parse(date).iso8601)
end

Cheers,
Ben
 
To view this discussion on the web visit https://groups.google.com/d/msg/datamapper/-/jKlzyXpXcVMJ.

DAZ

unread,
Oct 9, 2012, 5:04:35 PM10/9/12
to datam...@googlegroups.com
Thanks for the reply Ben, 

I'd suggest: 
def released_on=date
  super(Date.parse(date).iso8601)
end

that certainly looks more elegant, but I get an 'invalid date' argument error for that if I try to enter the date in the format "10/09/2012", any ideas?

cheers,

DAZ



On Tuesday, October 9, 2012 9:17:07 PM UTC+1, benlovell wrote:
Hi Daz,

On 9 October 2012 20:58, DAZ <daz...@gmail.com> wrote:
Hi Ben,

Thanks for the reply. This is what I thought was the case, but I just wondered if there were any nice DM methods that I didn't know about that parsed strings in a certain format, ready to insert into the database as a Date object.

I've found that the jQuery datepicker gives the date in this format 10/09/2012, so I have added the following method to the Song class to change it into a Date object:

  def released_on=date
    dates = date.split('/').map{ |s| s.to_i }
    super(Date.new(dates[2],dates[0],dates[1]))
  end



Ben Lovell

unread,
Oct 10, 2012, 5:16:10 AM10/10/12
to datam...@googlegroups.com
Hi Daz,

On 9 October 2012 22:04, DAZ <daz...@gmail.com> wrote:
Thanks for the reply Ben, 

I'd suggest: 
def released_on=date
  super(Date.parse(date).iso8601)
end

that certainly looks more elegant, but I get an 'invalid date' argument error for that if I try to enter the date in the format "10/09/2012", any ideas?


On an older ruby version? Try Date.strptime [0] or even better if you're trying to parse natural dates as well - the chronic gem [1]


Cheers,
Ben
 
To view this discussion on the web visit https://groups.google.com/d/msg/datamapper/-/PdSYABOhqw8J.
Reply all
Reply to author
Forward
0 new messages