time_select 空白字段保存表单提交时的默认时间 [英] time_select blank field saves a default time when form is submitted

查看:35
本文介绍了time_select 空白字段保存表单提交时的默认时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

  1. datetime_select 上没有值?
  2. 可选 time_select with allow_blank 默认为 00:00
  3. Rails time_select 设置默认值
  4. Rails 3:如何防止我的验证通过零时间"来自 time_select 下拉菜单?
  5. http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-time_select

这是我的代码:

_form.html.erb(为了简洁起见,只有我遇到问题的片段)<td><span style="display: block; width: auto; margin-left: 4%"><%= f.time_select :med1_time_of_day1, { include_blank: true, December_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, December_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, December_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, December_hour: true, minute_step: 15, ampm: true }, style: "width: 45%;"%></span></td><td>

<小时>

show.html.erb<table class="table table-bordered table-striped"><头><th>药物名称&使用说明<th>时间</th><th>服用药物的日期</th></thead><td><%=@med_record_form.med1_name %><%=@med_record_form.med1_instruct %></td><td><% 除非@med_record_form.med1_time_of_day1.nil?%><%= @med_record_form.med1_time_of_day1.strftime("%I:%M %p") %><br/><%结束%><% 除非@med_record_form.med1_time_of_day2.nil?%><%= @med_record_form.med1_time_of_day2.strftime("%I:%M %p") %><br/><%结束%><% 除非@med_record_form.med1_time_of_day3.nil?%><%= @med_record_form.med1_time_of_day3.strftime("%I:%M %p") %><br/><%结束%><% 除非@med_record_form.med1_time_of_day4.nil?%><%= @med_record_form.med1_time_of_day4.strftime("%I:%M %p") %><br/><%结束%></td></tbody>

<块引用>

注意:我也试过替换@instance_var.nil?和@instance_var.blank?和@instance_var.empty?没有成功.

<小时>

以防万一需要控制器...

 med_record_forms_controller.rb类 MedRecordFormsController <应用控制器before_filter :get_resident定义索引@med_record_form = @resident.med_record_forms结尾定义新@med_record_form = @resident.med_record_forms.build结尾定义创建@med_record_form = @resident.med_record_forms.build(params[:med_record_form])如果@med_record_form.saveredirect_to [@resident, @med_record_form] #, flash_class[:success] = "表格已创建!"别的render 'new' #, flash[:error] = "表单有问题"结尾结尾高清秀@med_record_form = @resident.med_record_forms.find(params[:id])结尾定义更新@med_record_form = @resident.med_record_forms.find(params[:id])如果@med_record_form.update_attributes(params[:med_record_form])flash[:success] = "表格已更新"redirect_to 控制器:'居民',动作:'show',id:params[:id]别的render 'edit', flash[:error] = "无法更新表单"结尾结尾定义编辑@med_record_form = @resident.med_record_forms.find(params[:id])结尾销毁@med_record_form = @resident.med_record_forms.find(params[:id])@med_record_form.destroyflash[:notice] = "你确定吗?"redirect_to resident_med_record_forms_path结尾私人的# get_resident 转换路由给定的resident_id# 进入一个@resident 对象,用于这个控制器 &对应观点def get_resident@resident = Resident.find(params[:resident_id])结尾结尾

解决方案

这个问题类似于:可选的 time_select with allow_blank 默认为 00:00

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

<块引用>

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

您需要像这样在控制器中将时间设置为零:

def 创建@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?&&参数[:med_record_form]["med1_time_of_day1(5i)"].blank?如果@med_record_form.saveredirect_to [@resident, @med_record_form] #, flash_class[:success] = "表格已创建!"别的render 'new' #, flash[:error] = "表单有问题"结尾结尾

Im having trouble understanding my options for time_select. My goal is to have only user created time selections render in my show action after form submission.

What is happening however is a default time of 12:00 AM being rendered for all time_select fields not touched by a user. I am looking for a way to either stop default time values from saving (which if I had to guess, isn't possible), or create a conditional that would allow me to prevent default time values from rendering.

I have looked over the following with no success so far:

  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

Here is my code:

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


And just in case the controller is needed...

    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

解决方案

This question is similar to this: Optional time_select with allow_blank defaults to 00:00

I found the default for time_select sets the time parts blank but there is a hidden date component that is not set to blank. You can see this when you look at the parameters:

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

You will need to set the time to nil in your controller like this:

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

这篇关于time_select 空白字段保存表单提交时的默认时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆