Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7978123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:24:21+00:00 2026-06-04T09:24:21+00:00

I’m using a datetime to save some appointments in a calendar. In this way

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T09:24:23+00:00Added an answer on June 4, 2026 at 9:24 am

    According to David suggestion I would create a virtual attribute start_at_hours then instead of putting the line inside the controller just edit the Model Event defining a before_update callback as:

    # inside your model
    before_update :set_hour
    
    def start_at_hours
      if !self.start_at.blank?
        self.start_at.hour
      end
    end
    
    def start_at_hours=(value)
    end
    
    # model code ...
    private
    def set_hour
      self.start_at = Time.parse(self.start_at.to_s) + self.start_at_hours.to_i.hours
    end
    

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I have some data like this: 1 2 3 4 5 9 2 6
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.