how to get the date in my database from my select date in the view?
I'm trying to figure out how to get and insert the date in my database using the select but开发者_运维技巧ton in my view : <%= select_date Date.today, :date %>
and in the migration I have a :date
coloumn.
thanks.
With the way that you have it you would have to set this explicitly
Date.civil(params[:date][:year].to_i, params[:date][:month].to_i, params[:date][:day].to_i)
to get the date.
However, an easier way to do this would be to change the select.
<%= select_date :model, :date %>
or
<%= f.select_date :date %>
depending on how you are expressing the form.
Then just add a before_create
method to the model to set the current date:
before_create :start_with_todays_date
def start_with_todays_date
self.date = Date.today
end
I would suggest you to read the section 2. Dealing with Model Objects of the form helpers guide or at least the ruby on rails starting guide to figure out how to build a simple model and write controller and view (include new/edit views which contains form) for it.
精彩评论