简体   繁体   English

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

[英]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 %>

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

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.

相关问题 在Rails中,如何从显示视图更新记录(而不是编辑)? - In Rails, how to update record from show view (and not edit)? 编辑表单删除数据库中的记录 - Edit Form Delete's a record from the database Rails 4 form_for关联-如何保存/编辑表单? - Rails 4 form_for associations - how to save/edit form? Rails:如何在编辑时将新值加载到表单中,而在保存时仅保留它们? - Rails: How can I load new values into a form on edit but only persist them on save? Android:如何在Android编程中编辑数据库中的特定记录(使用Ruby on Rails) - Android: How to edit specific record from database in Android Programming (Using Ruby on rails) 通过 Rails 控制台编辑数据库记录 - Edit a database record through the Rails console 如何在Rails中的另一个模型的“显示”页面中呈现“编辑”表单部分 - How to render a “Edit” form partial in the “Show” page of another model in Rails Rails:如何清除编辑表单? - Rails: How to clear an edit form? Rails 4嵌套表单未在show / edit上加载值 - Rails 4 nested form not loading values on show/edit 如何通过在 Ruby/Rails 中编辑数据库中的所有文章来修复我的编辑表单? - How do I fix my edit form from editing all the articles in my database in Ruby/Rails?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM