繁体   English   中英

如何仅显示记录的编辑按钮并将数据从Rails中的编辑表单保存到数据库

[英]How to show edit button only for a record and save data to database from edit form in rails

我正在以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

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

截图: 在此处输入图片说明

您需要为每个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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM