Hi Daz,
On 9 October 2012 20:58, DAZ <[email protected]> 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
>
> Not sure if that's the best way of doing it, but it certainly works!
>
> thanks for the help and feedback!
>
> DAZ
>
>
> On Tuesday, October 9, 2012 8:14:23 PM UTC+1, benlovell wrote:
>
>> On 9 October 2012 19:48, DAZ <[email protected]> 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
>>
>>
>>> 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
>>>
>>>
>>>
>>> On Sunday, October 7, 2012 2:07:30 PM UTC+1, benlovell wrote:
>>>
>>>> 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.
>>>>
>>>> http://trentrichardson.com/**exa**mples/timepicker/<http://trentrichardson.com/examples/timepicker/>
>>>>
>>>> Regards,
>>>> Ben
>>>>
>>>> Sent from my iPhone
>>>>
>>>> On 7 Oct 2012, at 11:48, DAZ <[email protected]> wrote:
>>>>
>>>> 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,**pa**rams[:month].to_i,params[:**
>>>> day]**.to_i)
>>>> song = Song.create(params[:song].**merg**e(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
>>>>
>>>> --
>>>> 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/**
>>>> ms**g/datamapper/-/OOhRt2vQKNEJ<https://groups.google.com/d/msg/datamapper/-/OOhRt2vQKNEJ>
>>>> .
>>>> To post to this group, send email to [email protected].
>>>> 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 <http://groups.google.com/group/datamapper?hl=en>.
>>>>
>>>> --
>>> 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/-/6zgiTuxv7zwJ<https://groups.google.com/d/msg/datamapper/-/6zgiTuxv7zwJ>
>>> .
>>>
>>> To post to this group, send email to [email protected].
>>> 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 <http://groups.google.com/group/datamapper?hl=en>
>>> .
>>>
>>
>> --
> 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/-/jKlzyXpXcVMJ.
>
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/datamapper?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"DataMapper" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/datamapper?hl=en.