向数据库迁移添加了列:条目未显示在SHOW视图中

时间:2019-01-28 23:30:05

标签: ruby-on-rails ruby controller

我在我的数据库表中添加了date的“运行”脚手架,该脚手架似乎可以正常工作,请参阅数据库模式文件:

create_table "runs", force: :cascade do |t|
    t.integer "user_id"
    t.string "Run_Type"
    t.string "Location"
    t.time "Start_Time"
    t.decimal "Pace"
    t.decimal "Miles"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.date "Run_Date"
    t.index ["user_id"], name: "index_runs_on_user_id"
  end

然后,我在NEW.html.erb文件的表单中添加了“日期”:

<%= form_with(model: run, local: true) do |form| %>
  <% if run.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(run.errors.count, "error") %> prohibited this run from being saved:</h2>

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



    <div class="form-group">
    <%= form.label :Run_Type %>
    <%= form.text_field :Run_Type, class: "form-control" %>
  </div>

   <div class="form-group">
    <%= form.label :Location %>
    <%= form.text_field :Location, class: "form-control" %>
  </div>

  <div class="form-group">
    <%= form.label :Run_Date %>
    <%= form.date_select :Run_Date, class: "form-control" %>
  </div>

   <div class="form-group">
    <%= form.label :Start_Time %>
    <%= form.time_select :Start_Time, class: "form-control" %>
  </div>

 <div class="form-group">
    <%= form.label :Pace %>
    <%= form.text_field :Pace, class: "form-control" %>
  </div>

  <div class="form-group">
    <%= form.label :Miles %>
    <%= form.text_field :Miles, class: "form-control" %>
  </div>

 <%= form.submit class: "btn btn-primary" %>
  </div>
<% end %>

一切似乎都可以正常工作,通过该表格,现在出现一个日期字段。但是,当我在数据库中创建或编辑条目以包含日期时,该条目不会显示在index.html.erb页面中,请参见下文:

<div class="container">
<h1>Runs</h1>

  <table class="table">
  <thead>
    <tr>
      <th>User</th>
      <th>Run Type</th>
      <th>Location</th>
      <th>Start Time</th>
      <th>Pace</th>
      <th>Miles</th>
      <th>Date</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @runs.each do |run| %>
      <tr>
        <td><%= run.user.try(:email) %></td>
        <td><%= run.Run_Type %></td>
        <td><%= run.Location %></td>
        <td><%= run.Start_Time %></td>
        <td><%= run.Pace %></td>
        <td><%= run.Miles %></td>
        <td><%= run.Run_Date %></td>
        <td><%= link_to 'Show', run %></td>
        <% if run.user == current_user %>
        <td><%= link_to 'Edit', edit_run_path(run) %></td>
        <td><%= link_to 'Destroy', run, method: :delete, data: { confirm: 'Are you sure?' } %></td>
         <% end %>
      </tr>
    <% end %>
  </tbody>
</table>

<br>
  <% if user_signed_in? %>
      <%= link_to 'New Run', new_run_path %>
  <% end %>

</div>

从视觉上看,请参见下面的屏幕抓取。尽管所有这些条目都已更新为包括日期,但它们现在显示在表格中:

enter image description here

1 个答案:

答案 0 :(得分:1)

在您的02:05 - 02:10 = 1中,您可能没有将新字段添加到列入白名单的参数中。

02:00 - 02:05 = 0