Я использую datetime для сохранения некоторых встреч в календаре. Таким образом, я могу хранить дату и время без учета двух значений.Datetime select определить диапазон часов
Я хочу, чтобы пользователи могли редактировать назначение и изменять дату и время. Дело в том, что я не могу оставлять пользователя свободным выбирать любой час, но любое назначение должно быть установлено в предопределенном диапазоне часов (24-часовой формат: 10, 12, 14, 16, 18, 20).
Поскольку в документации, которую я не мог найти способ, чтобы настроить datetime_select
Я на самом деле с помощью следующей формы:
.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
представляет собой массив, содержащий часы, которые я хочу, чтобы пользователи могли выбирать. При отправке этой формы действие обновления теряет информацию о времени, в результате чего datetime вроде: 2012-05-18 00:00:00
вместо использования часа, определенного в моем выборе.
Что я могу сделать, чтобы сделать эту работу необходимой мне?
Это контроллер, обратите внимание, что это вложенная ресурс 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
Пожалуйста, разместите свой код контроллера, который обрабатывает эту форму. –