user804968
user804968

Reputation: 181

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 button in my view : <%= select_date Date.today, :date %>

and in the migration I have a :date coloumn. thanks.

Upvotes: 1

Views: 329

Answers (2)

natedavisolds
natedavisolds

Reputation: 4295

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

Upvotes: 1

Adrien Coquio
Adrien Coquio

Reputation: 4930

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.

Upvotes: 0

Related Questions