How to make a hidden datetime selector?

Hi,

I am still very much a beginner at this - seems pretty neat though!

I have a nice little comments form like so:

Post a comment

<%= form_tag :action => "comment", :id => @post %> Name:
<%= text_field "comment", "name" %>
Date:
<%= datetime_select "comment", "created_at" %>
Email: (won't be displayed)
<%= text_field "comment", "email" %>
Website: (include http://)
<%= text_field "comment", "website" %>
Comment:
<%= text_area "comment", "body" %>
<%= submit_tag "Comment!" %>

Now, I don’t really want the user to be able to select a datetime, I’d
rather that it was done serverside, but I’ve no idea how to do that.

I toyed with replacing the datetime_select with a series of
hidden_fields, but that doesn’t seem the right way, and anyway, I
couldn’t work out how to get just the current year, just the day, just
the minute etc. Also, the prepopulated datetime isn’t correct, probably
due to me being in the UK and the server being in the US.

I would rather have the datetime added in the controller I think, is
this the correct way?

def comment
Post.find(params[:id]).comments.create(params[:comment])
flash[:notice] = “Added your comment.”
redirect_to :action => “show”, :id => params[:id]
end

Perhaps it should go in there somewhere…

Any ideas? Does anyone know of a good book/tutorial to get started on
such issues?

Thanks!

Hi, could you explain what you’re trying to do because this will allow
others to better assist you? It seems that you would like the datetime
set
automatically. If this is the case, then you want to remove the date
selection from the form because the created_at field will automatically
set
by the rails framework. Please read section, “Magic Column Names”, on
page
319 of AWDwRv2.
Good luck,

-Conrad

On Dec 23, 2007 1:24 PM, Richard Brashaw
[email protected]

Hi, I would highly recommend the following book:
Agile Web D. with Rails 2 Edition

You’ll want to complete Part II of this book and this will provide you
with
a good foundation to build from. Lastly, you can order the PDF and/or
paperback from

Good luck,

-Conrad

Don’t bother putting it in your form at all. You can do that server
side. The model is the best place for it, you can use a before filter
to execute all sorts of stuff, but in your case. In your post.rb model

before_create :set_time

def set_time
self.created_at = Time.now
end

In saying all this however, check if rails 2.0 automatically fills
this is for you. Try creating and saving a new record in the console,
as I’m pretty sure that Rails will fill in this field automatically.

over 'n out
Cam

Conrad T. wrote:

Hi, could you explain what you’re trying to do because this will allow
others to better assist you? It seems that you would like the datetime
set
automatically. If this is the case, then you want to remove the date
selection from the form because the created_at field will automatically
set
by the rails framework. Please read section, “Magic Column Names”, on
page
319 of AWDwRv2.
Good luck,

-Conrad

On Dec 23, 2007 1:24 PM, Richard Brashaw
[email protected]

That was amazingly simple - RoR is awesome!

I’ll read the book, sounds good.

Thanks for everyones help!