ruby-on-rails - time_select 空白字段保存表单提交时的默认时间

标签 ruby-on-rails ruby-on-rails-3 time-select

我无法理解 time_select 的选项。我的目标是在提交表单后只让用户创建的时间选择呈现在我的显示操作中。

然而,正在发生的事情是为所有未被用户触摸的 time_select 字段呈现默认时间 12:00 AM。我正在寻找一种方法来阻止默认时间值的保存(如果我不得不猜测,这是不可能的),或者创建一个条件来阻止默认时间值的呈现。

到目前为止,我已经查看了以下内容但没有成功:

  1. Nil value on datetime_select?
  2. Optional time_select with allow_blank defaults to 00:00
  3. Rails time_select set default
  4. Rails 3: How to prevent my validations to pass a "nil time" from time_select dropdowns?
  5. http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-time_select

这是我的代码:

_form.html.erb (only the snippet that I am having trouble with for brevity)


                    <td>
                    <span style="display: block; width: auto; margin-left: 4%">
                        <%= f.time_select :med1_time_of_day1, { include_blank: true, twelve_hour: true, minute_step: 15, ampm: true }, style: "width: 45%;" %>
                    </span>
                    <span style="display: block; width: auto; margin-left: 4%">
                        <%= f.time_select :med1_time_of_day2, { include_blank: true, twelve_hour: true, minute_step: 15, ampm: true }, style: "width: 45%;" %>
                    </span>
                    <span style="display: block; width: auto; margin-left: 4%">
                        <%= f.time_select :med1_time_of_day3, { include_blank: true, twelve_hour: true, minute_step: 15, ampm: true }, style: "width: 45%;" %>
                    </span>
                    <span style="display: block; width: auto; margin-left: 4%">
                        <%= f.time_select :med1_time_of_day4, { include_blank: true, twelve_hour: true, minute_step: 15, ampm: true }, style: "width: 45%;" %>
                    </span>
                </td>
                <td>

show.html.erb

<table class="table table-bordered table-striped">
        <thead>
            <th>Medication Name & Instructions for Use</th>
            <th>Time of Day</th>
            <th>
                Day(s) of the Month Medication was Taken
            </th>
        </thead>
        <tbody>
            <td>
                <%= @med_record_form.med1_name %>
                <%= @med_record_form.med1_instruct %>
            </td>
            <td>
                <% unless @med_record_form.med1_time_of_day1.nil? %>
                    <%= @med_record_form.med1_time_of_day1.strftime("%I:%M %p") %>
                    <br />
                <% end %>
                <% unless @med_record_form.med1_time_of_day2.nil? %>
                    <%= @med_record_form.med1_time_of_day2.strftime("%I:%M %p") %>
                    <br />
                <% end %>
                <% unless @med_record_form.med1_time_of_day3.nil? %>
                    <%= @med_record_form.med1_time_of_day3.strftime("%I:%M %p") %>
                    <br/>
                <% end %>
                <% unless @med_record_form.med1_time_of_day4.nil? %>
                    <%= @med_record_form.med1_time_of_day4.strftime("%I:%M %p") %>
                    <br />
                <% end %>
            </td>
        </tbody>
    </table>

Note: I've also tried replacing @instance_var.nil? with @instance_var.blank? and @instance_var.empty? without success.


以防万一需要 Controller ...

    med_record_forms_controller.rb

    class MedRecordFormsController < ApplicationController
  before_filter :get_resident

  def index
    @med_record_form = @resident.med_record_forms
  end

  def new
    @med_record_form = @resident.med_record_forms.build
  end

  def create
    @med_record_form = @resident.med_record_forms.build(params[:med_record_form])
    if @med_record_form.save
      redirect_to [@resident, @med_record_form] #, flash_class[:success] = "Form was created!"
    else
      render 'new' #, flash[:error] = "There was a problem with the form"
    end
  end

  def show
    @med_record_form = @resident.med_record_forms.find(params[:id])
  end

  def update
    @med_record_form = @resident.med_record_forms.find(params[:id])
    if @med_record_form.update_attributes(params[:med_record_form])
      flash[:success] = "Form updated"
      redirect_to controller: 'residents', action: 'show', id: params[:id]
    else
      render 'edit', flash[:error] = "Unable to update form"
    end
  end

  def edit
    @med_record_form = @resident.med_record_forms.find(params[:id])
  end

  def destroy
    @med_record_form = @resident.med_record_forms.find(params[:id])
    @med_record_form.destroy
    flash[:notice] = "You sure?"
    redirect_to resident_med_record_forms_path
  end

  private
  # get_resident converts the resident_id given by the routing
  # into an @resident object, for use in this controller & coresponding views
  def get_resident
    @resident = Resident.find(params[:resident_id])
  end
end

最佳答案

这个问题类似于:Optional time_select with allow_blank defaults to 00:00

我发现 time_select 的默认值将时间部分设置为空白,但有一个隐藏的日期组件未设置为空白。看参数就可以看出来了:

"my_time(1i)"=>"1", "my_time(2i)"=>"1", "my_time(3i)"=>"1", "my_time(4i)"=>"", "my_time(5i)"=>""

您需要像这样在 Controller 中将时间设置为 nil:

def create
  @med_record_form = @resident.med_record_forms.build(params[:med_record_form])
  @med_record_form.med1_time_of_day1 = nil if params[:med_record_form]["med1_time_of_day1(4i)"].blank? && 
        params[:med_record_form]["med1_time_of_day1(5i)"].blank?
  if @med_record_form.save
    redirect_to [@resident, @med_record_form] #, flash_class[:success] = "Form was created!"
  else
    render 'new' #, flash[:error] = "There was a problem with the form"
  end
end

关于ruby-on-rails - time_select 空白字段保存表单提交时的默认时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14367705/

相关文章:

ruby - 如何在亚马逊实例上安装 redmine?

ruby-on-rails - 时间为 Rails 3 选择 12 小时格式的表单助手?

ruby-on-rails - 在Ruby on Rails中使用12小时时间和时区的time_select

ruby-on-rails - ruby rails : time_select default value

ruby-on-rails - 使用 commontator gem 进行注释时出现 "Undefined local variable or method"错误

ruby-on-rails-3 - 如何在 Rails 3 中干燥路由

ruby-on-rails - 使足智多谋在 Controller 中起什么作用?

ruby-on-rails - 通过 AJAX 创建表单时显示错误消息

ruby-on-rails - 如果没有删除链接,则进行销毁的 Rspec 测试

ruby-on-rails - 为什么参数比 HTTP URL 中的查询字符串更不常用?