【问题标题】:Building a datetime in a form_for without using datetime_select in rails, how to send params在 form_for 中构建日期时间而不在​​ rails 中使用 datetime_select,如何发送参数
【发布时间】:2015-03-06 09:05:46
【问题描述】:

我到处搜索,我认为解决方案很简单,但我过于复杂了。

基本上我有一个具有以下属性的约会模型:

--- !ruby/object:Appointment
attributes:
  id: 
  time: 
  user_id: 
  created_at: 
  updated_at: 

时间:是一个日期时间属性

在控制器中,我有一个标准的创建操作:

class AppointmentsController < ApplicationController
  def create
     @appointment = Appointment.create(appointment_params)
     current_user.appointment = @appointment

     if @appointment.save
       flash[:success] = "Appointment Booked!"
       redirect_to appointments_path
     else
       redirect_to appointments_path
     end

  private
    def appointment_params
      params.require(:appointment).permit(:time, :test1, :test2)
    end
  end

在创建视图中,我有一个带有以下代码的表单:

<%= form_for(@appointment) do |f| %>

  <div class="field">
    <%= f.label :time %><br>
    <%= f.datetime_select(:time, {start_year: 2015, end_year: 2016, start_hour: 9, end_hour: 18, minute_step: 60}) %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

现在这一切都保存得很好,可以做我想做的事,用户使用日期时间选择器创建约会,保存没有问题。

但是,当涉及到样式时,它变得很困难,因为我不知道如何分解 datetime_select。

我想要做的是使用我找到的 jQuery 日期时间选择器,但我不知道如何将参数传递给我的控制器,以便构建一个能够保存到 时间的日期时间: 属性

【问题讨论】:

  • 我不明白你的问题,你想删除默认日历并使用 JQuery 日历吗?
  • 嘿,如果这个问题让您感到困惑,对不起!基本上我想学习如何在不使用 rails 中的 datetime_select 帮助器的情况下建立一个日期时间。
  • 你使用的是formtastic还是类似的?
  • 不,正如您在代码中看到的那样,我只是使用 form_for 帮助器

标签: jquery ruby-on-rails ruby datetime form-for


【解决方案1】:

有很多方法可以做到这一点,其中一种是使用简单的 f.textbox,并在此输入上使用 JQuery datepicker。

更专业的方法是覆盖 datetime_select。

一个例子:

在 app/inputs 中创建一个 DatePickerInput

module SimpleForm
  module Inputs
    class DateTimePickerInput < Base
      def input
        @builder.text_field(attribute_name,input_html_options)
      end    
    end
  end
end

使用这个$("input.date_time_picker").datepicker(); 创建一个 JQuery Datepicker

用于使用 &lt;%= f.input :deadline, :as =&gt; :date_time_picker %&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-11
    相关资源
    最近更新 更多