[英]How to show edit button only for a record and save data to database from edit form in rails
I am displaying all the records from db in the form of HTML table along with edit button. 我正在以HTML表格的形式显示来自db的所有记录以及“编辑”按钮。 On, click of edit button it redirects to edit view which will display the additional columns.
在打开时,单击编辑按钮,它将重定向到编辑视图,该视图将显示其他列。
Here, I was showing save button in edit form for all the rows/records. 在这里,我正在以编辑形式显示所有行/记录的保存按钮。 Is there a way to prevent so that I can just show to the one I selected in index form.
有没有一种方法可以防止这样,以便我可以显示给我以索引形式选择的那个。
Also, after updating the fields how do I save the record and redirect to index view. 此外,更新字段后,如何保存记录并重定向到索引视图。
Please find the code snippets below: 请在下面找到代码片段:
metrics_controller.rb: 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
edit.html.rb: edit.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 %>
You need to have a form
for each metric
: 您需要为每个
metric
一个form
:
<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| %>
<%= form_for :metrics_controller, url: metric_path(data), method: :patch do |f| %>
<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>
</tr>
<% end %>
<% end %>
</table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.