I have a Rails 4 application and am trying to have an admin select appointment times.
...
<div class="field">
<%= f.label :start_time %><br>
<%= f.datetime_select :start_time %>
</div>
<div class="field">
<%= f.label :end_time %><br>
<%= f.datetime_select :end_time %>
</div>
...
My model doesn't do anything with the date except validate it's presence. In my database (Sqlite), it stores them as time objects.
My controller:
def create
@user = User.find(params[:user_id])
@appointment = @user.appointments.build(appointment_params)
...
end
...
def appointment_params
params.require(:appointment).permit(:start_time, :end_time, :comments)
end
I have changed the default time zone to:
config.time_zone = "Pacific Time (US & Canada)"
Problem
I create via the form and then check in the Rails console like this:
Loading development environment (Rails 4.2.2)
>> appointment = Appointment.all.last
Appointment Load (0.2ms) SELECT "appointments".* FROM "appointments" ORDER BY "appointments"."id" DESC LIMIT 1
=> #<Appointment id: 8, user_id: 1, comments: "", start_time: "2000-01-01 12:50:00", end_time: "2000-01-01 12:50:00">
>> appointment.start_time.year
=> 2000
I get this even though I selected (and it's by default) 2015. Why is Rails storing the year as always 2000?
Edit - Migration
class ChangeTimeInAppointments < ActiveRecord::Migration
def change
remove_column :appointments, :time
add_column :appointments, :start_time, :time
add_column :appointments, :end_time, :time
end
end
Schema:
create_table "appointments", force: :cascade do |t|
t.integer "user_id"
t.text "comments"
t.time "start_time"
t.time "end_time"
end
Aucun commentaire:
Enregistrer un commentaire