Rails SELECT场未显示发射值



当我创建对象时,值在db内显示正确;但是,当我尝试编辑值的选择标签值时,它们看起来错误。

我有不同的编辑形式:

<%= form_for(@opening_hour, url: store_opening_hours_update_path) do |form| %>

  <div class="form-group">
    <%= form.select :day, [['Select a day'], ['Monday', 1], ['Tuesday', 2], ['Wednesday', 3],
                         ['Thursday', 4], ['Friday', 5], ['Saturday', 6],['Sunday', 7]], {}, class: "form-control" %>
  </div>
  <div class="form-group">
    <%= form.select :opens, [['Select opening hour'],['12:00', 12], ['13:00', 13], ['14:00', 14], ['15:00', 15], ['16:00', 16], ['17:00', 17],
                           ['18:00', 18], ['19:00', 19], ['20:00', 20], ['21:00', 21], ['22:00', 22], ['23:00', 23], ['24:00', 24],
                           ['01:00', 1], ['02:00', 2], ['03:00', 3], ['04:00', 4], ['05:00', 5], ['06:00', 6], ['07:00', 7],
                           ['08:00', 8], ['09:00', 9], ['10:00', 10], ['11:00', 11]], {}, class: "form-control"  %>
  </div>

在控制器中:

def edit
  @opening_hour = OpeningHour.find(params[:id])
end

我编辑并重新打开编辑表单后,值将看起来正确。知道为什么会发生这种情况吗?

更新1

before_action :set_opening_hour, only: [:edit, :update]
def create
  @opening_hour = OpeningHour.create(opening_hour_params)
    respond_to do |format|
      if @opening_hour.save
        format.html { redirect_to store_opening_hours_index_path and return}
        flash[:notice] = "Hours were successfully created."
      else
        format.html { render :new }
        format.json { render json: @opening_hour.errors, status: :unprocessable_entity }
      end
    end
end
def update
  respond_to do |format|
    if @opening_hour.update(opening_hour_params)
      format.html { redirect_to store_opening_hours_index_path and return}
      flash[:notice] = "Hours were successfully updated."
    else
      format.html { render :edit }
      format.json { render json: @opening_hour.errors, status: :unprocessable_entity }
    end
  end
end
private
def set_opening_hour
  @opening_hour = OpeningHour.find(params[:id])
end
def opening_hour_params
  params.require(:opening_hour).permit(:user_id, :day, :closes, :opens, :valid_from, :valid_through)
end
end

编辑表格:

<div class="col-md-5 col-md-offset-3">
    <%= form_for(@opening_hour, url: store_opening_hours_update_path) do |form| %>
    <% if @opening_hour.errors.any? %>
      <div class="centerList">
      <div id="error_explanation">
        <h2><%= pluralize(@opening_hour.errors.count, "error") %> prohibited the business hours from being saved:</h2>
        <% @opening_hour.errors.full_messages.each do |message| %>
          <li><%= message %></li>
        <% end %>
      </div>
    </div>
    <br>
  <% end %>

  <div class="form-group">
   <%= form.select :day, [['Select a day'], ['Monday', 1], ['Tuesday', 2], ['Wednesday', 3],
                          ['Thursday', 4], ['Friday', 5], ['Saturday', 6],['Sunday', 7]], {}, class: "form-control" %>
</div>

<div class="form-group">
  <%= form.select :opens, [['Select opening hour'],['12:00', '12:00'], ['13:00', 13], ['14:00', 14], ['15:00', 15], ['16:00', 16], ['17:00', 17],
                           ['18:00', 18], ['19:00', 19], ['20:00', 20], ['21:00', 21], ['22:00', 22], ['23:00', 23], ['24:00', 24],
                           ['01:00', 1], ['02:00', 2], ['03:00', 3], ['04:00', 4], ['05:00', 5], ['06:00', 6], ['07:00', 7],
                           ['08:00', 8], ['09:00', 9], ['10:00', 10], ['11:00', '11:00']], {}, class: "form-control"  %>
</div>

<div class="form-group">
  <%= form.select :closes, [['Select closing hour'],['12:00', 12], ['13:00', 13], ['14:00', '14:00'], ['15:00', 15], ['16:00', 16], ['17:00', '17:00'],
                           ['18:00', 18], ['19:00', 19], ['20:00', 20], ['21:00', 21], ['22:00', 22], ['23:00', 23], ['24:00', 24],
                           ['01:00', 1], ['02:00', 2], ['03:00', 3], ['04:00', 4], ['05:00', 5], ['06:00', 6], ['07:00', 7],
                           ['08:00', 8], ['09:00', 9], ['10:00', 10], ['11:00', '11:00']], {}, class: "form-control"  %>
</div>

  <div class="form-group">
    <%= form.submit t("opening_hours_index_6"), :class=>"btn btn-primary" %>
  </div>
<% end %>
<div id="fixed-bottom-spacing" style="height: 60px;">&nbsp;</div>

只是尝试:

<div class="form-group">
   <%= form.select :day, [['Select a day'], ['Monday', 1], ['Tuesday', 2], ['Wednesday', 3],['Thursday', 4], ['Friday', 5], ['Saturday', 6],['Sunday', 7]],  class: "form-control" %>
</div>

第二支架是问题。

我假设问题是,当您编辑记录时,在下拉列表中未选择"打开"的正确值。

这是由选择值与数据库中的值之间的不匹配引起的。

您正在恢复数据库的时间,并试图将其与下拉列表中的整数值匹配。例如['12:00',12]将将值存储在数据库中,并期望返回 12 。由于列是时间数据类型,当您编辑记录时,返回的值不是下拉选项所要求的12个。

您需要更改数据库列类型或更改下拉选项中的值。选择将取决于您的业务案例。

最新更新