【问题标题】:How to show edit button only for a record and save data to database from edit form in rails如何仅显示记录的编辑按钮并将数据从rails中的编辑表单保存到数据库
【发布时间】:2018-11-13 18:47:32
【问题描述】:

我正在以 HTML 表格的形式显示来自 db 的所有记录以及编辑按钮。在,单击编辑按钮,它会重定向到编辑视图,该视图将显示附加列。

在这里,我在编辑表单中显示了所有行/记录的保存按钮。有没有办法防止这样我可以只显示给我以索引形式选择的那个。

另外,更新字段后如何保存记录并重定向到索引视图。

请在下面找到代码sn-ps:

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

截图:

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 仍然有同样的问题
    • @MallelaSriPrakash 以上必须工作,请您报告上述更改或控制器操作正在处理的错误。
    • 我仍然可以看到所有记录的保存按钮。保存记录后,我收到以下错误: ActionController::ParameterMissing in MetricsController#update param is missing or the value is empty: metric Parameters: {"utf8"=>"✓", "_method"=>"patch" , "authenticity_token"=>"iF1pEruUPuX9uRSv5ubNpvBRXNOrbfcV+E9iadp7JmPYjIQpfV955E3Nx3TMMtFvNLH2SUVUwcseZnsylbeuaw==", "metrics_controller"=>{"IsValid"=>"", "Comments"=>"Valid"}, "commit"=>"Save", " =>"Metric1"}
    • 能够整理出来。我正在迭代所有记录而不是特定记录。更新了代码以仅显示单个记录的数据,问题现已解决。
    猜你喜欢
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-26
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    • 2015-06-13
    相关资源
    最近更新 更多