I’m using a datetime to save some appointments in a calendar.
In this way I can store togheter date and time without have the two values distinct.
What I’d like is to allow users to edit the appointment and change date/time.
The fact is that I cannot leave the user free to select any hour but any appointment must be set in a predefined range of hours (24h format: 10, 12, 14, 16, 18, 20).
Since in the documentation I could not find any way to customize datetime_select I’m actually using the following form:
.control-group
= f.label :start_at, :class => 'control-label'
.controls
= f.date_select :start_at, :value => f.object.start_at, :class => "datetimefield"
.control-group
= f.label :start_at, :class => 'control-label'
.controls
= f.select :start_at, :value => Event::ALLOWED_HOURS, :class => 'number_field'
Event::ALLOWED_HOURS is an array containing the hours I want to let users choose from.
When submitting this form the update action loses the timing info, resulting in a datetime like: 2012-05-18 00:00:00 instead of using the hour defined in my select.
What can I do to make this work as I need?
This is the controller, please note that this is a nested resource of EventCalendar:
class EventsController < ApplicationController
before_filter :authenticate_user!
load_and_authorize_resource
# GET /events
# GET /events.json
def index
respond_to do |format|
format.html # index.html.erb
format.json { render json: @events }
end
end
# GET /events/1
# GET /events/1.json
def show
@event_calendar = EventCalendar.find(params[:event_calendar_id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @event }
end
end
# GET /events/new
# GET /events/new.json
def new
@event_calendar = EventCalendar.find(params[:event_calendar_id])
@start_at = Time.parse("#{params[:day]} #{params[:hour]}")
respond_to do |format|
format.html # new.html.erb
format.json { render json: @event }
end
end
# GET /events/1/edit
def edit
@event_calendar = EventCalendar.find(params[:event_calendar_id])
end
# POST /events
# POST /events.json
def create
@event_calendar = EventCalendar.find(params[:event_calendar_id])
@start_at = params[:start_at]
respond_to do |format|
if @event.save
format.html { redirect_to root_path, notice: 'Appuntamento creato con successo.' }
format.json { render json: @event = EventCalendar.find(params[:event_calendar_id]), status: :created, location: @event }
else
format.html { render action: "new" }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
end
end
# PUT /events/1
# PUT /events/1.json
def update
@event_calendar = EventCalendar.find(params[:event_calendar_id])
respond_to do |format|
if @event.update_attributes(params[:event])
format.html { redirect_to root_path, notice: 'Appuntamento aggiornato con successo.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
end
end
# DELETE /events/1
# DELETE /events/1.json
def destroy
@event_calendar = EventCalendar.find(params[:event_calendar_id])
@event.destroy
respond_to do |format|
format.html { redirect_to root_url, notice: 'Appuntamento cancellato con successo.'}
format.json { head :no_content }
end
end
end
According to David suggestion I would create a virtual attribute
start_at_hoursthen instead of putting the line inside the controller just edit the ModelEventdefining abefore_updatecallback as:This should work as you need without exceptions.
It’s doing the same thing as David explanation, but directly in the model without the need to change your controller.
Edit: by defining a getter and a setter you can also set the default value for your select (the setter is not needed in this case, but I put it anyway).