我以 HTML 表格的形式显示来自 db 的所有记录以及编辑按钮。打开,单击编辑按钮,它重定向到编辑视图,该视图将显示附加列。
在这里,我在编辑表单中显示了所有行/记录的保存按钮。有没有办法防止这样我可以只显示我以索引形式选择的那个。
此外,更新字段后,如何保存记录并重定向到索引视图。
请在下面找到代码片段:
metrics_controller.rb:
class MetricsController < ApplicationController
def index
@metricAll = Metric.all
end
def show
@metric = Metric.find(params[:id])
end
def edit
@metric = Metric.find(params[:id])
@metricAll = Metric.all
end
def create
@metric = Metric.new(post_params)
if(@metric.save)
redirect_to @metric
else
render 'new'
end
end
def update
@metric = Metric.find(params[:id])
if(@metric.update_attributes(post_params))
redirect_to @metric
else
render 'edit'
end
end
private def post_params
params.require(:metric).permit(:Metric, :WI, :Value, :UT, :Score, :IsValid, :UserName, :Comments)
end
end
编辑.html.rb:
<%= form_for :metrics_controller, url: metric_path(@metric), method: :patch do |f| %>
<table id="metrics">
<thead>
<tr id="AllMetricColumnNames">
<th id="Metric"><div>Metric</th>
<th id="WI">WI</th>
<th id="Value">Value</th>
<th id="UT">UT</th>
<th id="Score">Score</th>
<th id="IsValid">IsValid</th>
<th id="UserName">UserName</th>
<th id="Comments">Comments</th>
<th id="EditColumn">Edit</th>
</tr>
</thead>
<% @metricAll.each do |data| %>
<tr id="AllMetricValues">
<td id="Value"><%= data.Metric %></td>
<td id="WI"><%= data.WI %></td>
<td id="Value"><%= data.Value %></td>
<td id="UT"><%= data.UT %></td>
<td id="Score"><%= data.Score %></td>
<td><%= f.select :IsValid, options_for_select(['True', 'False']), :include_blank => true, :class => 'chosen-select', :required => true, value: data.IsValid %></td>
<td id="UserName"><%= data.UserName %></td>
<td id="Comments"><%= f.text_field :Comments, value: data.Comments %></td>
<td id="SaveButton"><%= f.submit "Save" %></td>
<% end %>
</tr>
</table>
<% end %>
