Ruby on rails 向数据库迁移添加了列:条目未显示在显示视图中
我在DB表中添加了Ruby on rails 向数据库迁移添加了列:条目未显示在显示视图中,ruby-on-rails,ruby,controller,Ruby On Rails,Ruby,Controller,我在DB表中添加了date,用于“Run”scaffold,这似乎有效,请参见DB模式文件: 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 "crea
date
,用于“Run”scaffold,这似乎有效,请参见DB模式文件:
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文件的表单中添加了“date”:
<%= 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>
跑
使用者
运行类型
地方
开始时间
速度
英里
日期
作为视觉效果,请参见下面的屏幕抓图。虽然所有这些条目都已更新以包含日期,但它们现在显示在表中:
在您的
运行\u controller.rb中,您可能没有将新字段添加到白名单参数中
# Never trust parameters from the scary internet, only allow the white list through.
def run_params
params.require(:run).permit(... other fields... , :date)
end
是的,这很有效,哇。谢谢