Rails:如何使用time_select来节省价值

Sam*_*lue 10 ruby-on-rails ruby-on-rails-4

我想补充time_selectinclude_blank.我这样做:

    <%= f.time_select :start_at, include_blank: true, ampm: true %><br>
Run Code Online (Sandbox Code Playgroud)

我想要做的是删除值(保存为零?)如果在视图中选择了空白.

虽然我尝试了以下帖子,但它对我不起作用.

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

带allow_blank的可选time_select默认为00:00

1)当我尝试如下时,没有出现错误,但00:00:00保存.

调节器

  def update
    @event = Event.find(params[:id])

    if event_params["start_at(4i)"].blank? or event_params["start_at(5i)"].blank?
      @event.start_at = nil
    end

    if @event.update(event_params)
      flash[:success] = "event updated!"
      redirect_to root_url
    else
      render 'edit'
    end
  end
Run Code Online (Sandbox Code Playgroud)

2)当我尝试如下(更改if子句)时,不会出现错误,但00:00:00会保存.

调节器

  def update
    @event = Event.find(params[:id])

    if params[:id]["start_at(4i)"].blank? or params[:id]["start_at(5i)"].blank?
      @event.start_at = nil
    end

    if @event.update(event_params)
      flash[:success] = "event updated!"
      redirect_to root_url
    else
      render 'edit'
    end
  end
Run Code Online (Sandbox Code Playgroud)

3)当我尝试如下(添加before_action)时,没有出现错误,但00:00:00保存.

调节器

  before_action :blank_time,     only: [:update]

  def update
    @event = Event.find(params[:id])

    if @event.update(event_params)
      flash[:success] = "event updated!"
      redirect_to root_url
    else
      render 'edit'
    end
  end

  private

    def blank_time
      if params[:id]["start_at(4i)"].blank? or params[:id]["start_at(5i)"].blank?
        params[:id]['start_at(1i)'] = ""
        params[:id]["start_at(2i)"] = ""
        params[:id]["start_at(3i)"] = ""
        params[:id]["start_at(4i)"] = ""
        params[:id]["start_at(5i)"] = ""
      end
    end
Run Code Online (Sandbox Code Playgroud)

4)当我尝试如下(使用nil而不是"")时,出现错误.

错误

IndexError (string not matched):
  app/controllers/events_controller.rb:106:in `[]='
  app/controllers/events_controller.rb:106:in `blank_time'
Run Code Online (Sandbox Code Playgroud)

调节器

  before_action :blank_time,     only: [:update]

  def update
    @event = Event.find(params[:id])

    if @event.update(event_params)
      flash[:success] = "event updated!"
      redirect_to root_url
    else
      render 'edit'
    end
  end

  private

    def blank_time
      if params[:id]["start_at(4i)"].blank? or params[:id]["start_at(5i)"].blank?
        params[:id]['start_at(1i)'] = nil
        params[:id]["start_at(2i)"] = nil
        params[:id]["start_at(3i)"] = nil
        params[:id]["start_at(4i)"] = nil
        params[:id]["start_at(5i)"] = nil
      end
    end
Run Code Online (Sandbox Code Playgroud)

如果您能给我任何建议,我们将不胜感激.

UPDATE

虽然我在events_controller.rb下面更改编辑,但ActiveModel::MissingAttributeError (can't write unknown attribute 'start_at(4i)'):会显示错误.

  def edit
    @room = Room.find(params[:room_id])
    @event = @room.events.find(params[:id])

    @event['start_at(4i)'] = @event.start_at.split(':')[0]   #the error occur here
    @event['start_at(5i)'] = @event.start_at.split(':')[1]
  end
Run Code Online (Sandbox Code Playgroud)

Pra*_*ota 6

我的想法是这样的:

移民:

class CreateTests < ActiveRecord::Migration[5.0]
def change
create_table :tests do |t|
  t.string :time

  t.timestamps
end
end
end
Run Code Online (Sandbox Code Playgroud)

形成:

<%= form_for(test) do |f| %>
<% if test.errors.any? %>
<div id="error_explanation">
  <h2><%= pluralize(test.errors.count, "error") %> prohibited this test from being saved:</h2>

  <ul>
  <% test.errors.full_messages.each do |message| %>
    <li><%= message %></li>
  <% end %>
  </ul>
</div>
<% end %>

<div class="field">
<%= f.label :time %>
<%= f.time_select :time, include_blank: true, ampm: false %><br>
</div>

<div class="actions">
 <%= f.submit %>
 </div>
<% end %>
Run Code Online (Sandbox Code Playgroud)

控制器: 这将保存:当值发送为空时,您可以使用条件来检查参数是否为空或设置时间值.//这耗费了很多时间,我跳过了你完成.

def create
@test = Test.new(test_params)
@time = (params[:test]['time(4i)']).to_s
@time_ampm = (params[:test]['time(5i)']).to_s
@test.time = @time+":"+ @time_ampm

respond_to do |format|
  if @test.save
    format.html { redirect_to @test, notice: 'Test was successfully created.' }
    format.json { render :show, status: :created, location: @test }
  else
    format.html { render :new }
    format.json { render json: @test.errors, status: :unprocessable_entity }
  end
end
end
Run Code Online (Sandbox Code Playgroud)

用于更新

def edit
 @test = Test.find(params[:id])
 @test['time(4i)'] = @test.time.split(':')[0]
 @test['time(5i)'] = @test.time.split(':')[1]
end

def update
  @test = Test.find(params[:id])
  @time = (params[:test]['time(4i)']).to_s
  @time_ampm = (params[:test]['time(5i)']).to_s
  @test.time = @time+":"+ @time_ampm
  @test.update(test_params)
end
Run Code Online (Sandbox Code Playgroud)


Iva*_*van 2

分配给 nil 不会执行任何操作,因为调用 时会使用@event.starts_at中的属性,覆盖您的初始分配。#event_params#update

覆盖参数中的starts_at 属性应该可以。

def update
    @event = Event.find(params[:id])

    if event_params["start_at(4i)"].blank? or event_params["start_at(5i)"].blank?
      event_params = event_params.reject { |k, v| k.starts_with? 'starts_at' }
                                 .merge(starts_at: nil)
    end

    if @event.update(event_params)
      flash[:success] = "event updated!"
      redirect_to root_url
    else
      render 'edit'
    end
end
Run Code Online (Sandbox Code Playgroud)

starts_at(1i)以下行查找并删除to的参数starts_at(5i),然后将整个starts_at属性设置为 nil:

event_params.reject { |k, v| k.starts_with? 'starts_at' }.merge(starts_at: nil)