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

时间:2018-11-13 18:47:33

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4

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

截屏: enter image description here

1 个答案:

答案 0 :(得分:1)

每个form必须有一个metric

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