Mallela SriPrakash
Mallela SriPrakash

Reputation: 69

Facing issues with link_to "Edit" in rails

I am trying to display all the metrics in a table format with an edit option. But, I end up with the below error

enter image description here

In Index view, I am able to see all the data. But when I click on edit link, it is not redirecting to edit view where I have different columns to be showed.

index view:

<%= form_for :metrics_controller, url: metrics_path(@metric), method: :get do |f| %>
  <table id="metrics">
    <thead>
    <tr id="AllMetricColumnNames">
      <th id="CommentsColumn">Comments</th>
      <th id="EditColumn">Edit</th>
    </tr>
    </thead>
    <% @metricAll.each do |data| %>
      <tr id="AllMetricValues">
        <td id="Comments"><%= data.Comments %></td>
        <td id="EditButton"><%= link_to "Edit", edit_metric_path(@metricAll) %></td>
    <% end %>
    </tr>
  </table>
<% end %>

Controller:

class MetricsController < ApplicationController
  def index
    @metricAll = Metric.all
  end

  def show
    @metric = Metric.find(params[:id])
  end

  def edit
    @metric = Metric.find(params[:id])
  end

  private def post_params
    params.require(:metric).permit(:Metric, :Comments)
  end
end

routes:

  root 'metrics#index'
  get 'index' => 'metrics#index'
  get 'edit' => 'metrics#edit'
  resources :metrics

Upvotes: 0

Views: 97

Answers (2)

Frank Etoundi
Frank Etoundi

Reputation: 356

According to your screenshot, the error is within the model. Also, as mentioned by others, you should remove those get routes as the resources :metrics will generate the necessary routes for all your CRUD actions a.ka. for the index, show, edit, new, create, update, destroy.

My guess is that the metric.rb file has a belongs_to :automated_thresholding relationship but the metrics database table is missing the field automated_thresholding_id.

You should create a migration to add that field

add_reference :metrics, :automated_thresholding

Upvotes: 0

Ursus
Ursus

Reputation: 30056

You're passing ALL the metrics for the edit route. Move from

<td id="EditButton"><%= link_to "Edit", edit_metric_path(@metricAll) %></td>

to

<td id="EditButton"><%= link_to "Edit", edit_metric_path(data) %></td>

data is the current metric in your code

Upvotes: 1

Related Questions