ruby-on-rails – Rails:如何使用time_select保存值

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – Rails:如何使用time_select保存值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图添加time_select与include_blank.我这样做:
<%= f.time_select :start_at,include_blank: true,ampm: true %><br>

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

虽然我尝试了以下职位,但对我来说并不奏效.

time_select blank field saves a default time when form is submitted

Optional time_select with allow_blank defaults to 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

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

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

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

错误

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

调节器

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

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

UPDATE

虽然我在以下更改了events_controller.rb中的编辑,但是显示错误ActiveModel :: MissingAttributeError(不能写入未知属性’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

解决方法

我的想法是这样的:

移民:

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

  t.timestamps
end
end
end

形成:

<%= 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,ampm: false %><br>
</div>

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

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

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

为了更新

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

猜你在找的Ruby相关文章