Rails选择字段未显示当前值

时间:2018-02-26 09:24:46

标签: ruby-on-rails

当我创建对象时,数值在数据库中显示正确;但是,当我尝试编辑值时,选择标记值显示错误。

我有一个不同的编辑形式:

<%= 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>

2 个答案:

答案 0 :(得分:0)

试试:

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

第二个括号是问题。

答案 1 :(得分:0)

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

这是由select中提供的值与数据库中的值不匹配引起的。

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

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