Bootstrap 3 Datetimepicker with Rails form_for

本秂侑毒 提交于 2019-12-12 17:07:27

问题


I am a new Rails developer and I am having trouble using the bootstrap3 datetimepicker together with my rails form_for to save to a DateTime attribute in my ScheduledAccess Model. The one I am using is from http://eonasdan.github.io/bootstrap-datetimepicker/

Code:

.form-group
  = f.label :start_time
  datetimepicker1.input-group.date

    = f.text_field :start_time, class:"form-control", id:"scheduled_access_start_time"
    span.input-group-addon
      span.glyphicon.glyphicon-calendar

.form-group
  = f.label :end_time
  datetimepicker2.input-group.date
    = f.text_field :start_time, class:"form-control", id:"scheduled_access_start_time"
    span.input-group-addon
      span.glyphicon.glyphicon-calendar

javascript:
  $(function () {

    $('#datetimepicker1').datetimepicker(
    {
      sideBySide: true
    });
    $('#datetimepicker2').datetimepicker(
    {
      sideBySide: true
    });

  });

_fields.html.slim

Do note that I am able to save to my ScheduledAccess model when I use the default Rails/HTML5 datetime as shown below.

Code:

.form-group
  = f.label :start_time
  = f.datetime_local_field :start_time, class: "form-control"

.form-group
  = f.label :end_time
  = f.datetime_local_field :end_time, class: "form-control"

The following code shows how I code my ScheduledAccess controller which might be needed.

Code:

class ScheduledAccessesController < ApplicationController
  before_filter :authenticate_user!

  def create
    @access = ScheduledAccess.new(scheduledaccess_params)
    @access.user_id = current_user.id

    # generate 5-digit random pin
    randompin = rand(10**5)
    @access.pin = randompin


    if @access.save
      redirect_to home_path
    else
      flash[:alert] = "There was some unexpected error! Please Retry!"
      redirect_to new_scheduled_access_path
    end
  end

  def destroy
  end

  def edit
    @access = current_access
  end

  def new
    @access = ScheduledAccess.new
  end


  private

    def scheduledaccess_params
      params.require(:scheduled_access).permit(:name,:pin, :phoneno, :start_time, :end_time, :remarks)
    end

    def current_access
      ScheduledAccess.find(params[:id])
    end
end

scheduled_accesses_controller.rb

When I use the bootstrap3 datetimepicker and try to save. I always get the flash messsage.

Any help would be appreciated! =)

来源:https://stackoverflow.com/questions/31623469/bootstrap-3-datetimepicker-with-rails-form-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!