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