Facing issues with link_to “Edit” in rails










0















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









share|improve this question






















  • Why did you add get routes ? I think resources :metrics would be enough if you want to use default rails routes pattern

    – t s
    Nov 13 '18 at 13:26
















0















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









share|improve this question






















  • Why did you add get routes ? I think resources :metrics would be enough if you want to use default rails routes pattern

    – t s
    Nov 13 '18 at 13:26














0












0








0








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









share|improve this question














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






ruby-on-rails ruby ruby-on-rails-4






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 13:17









Mallela SriPrakashMallela SriPrakash

295




295












  • Why did you add get routes ? I think resources :metrics would be enough if you want to use default rails routes pattern

    – t s
    Nov 13 '18 at 13:26


















  • Why did you add get routes ? I think resources :metrics would be enough if you want to use default rails routes pattern

    – t s
    Nov 13 '18 at 13:26

















Why did you add get routes ? I think resources :metrics would be enough if you want to use default rails routes pattern

– t s
Nov 13 '18 at 13:26






Why did you add get routes ? I think resources :metrics would be enough if you want to use default rails routes pattern

– t s
Nov 13 '18 at 13:26













2 Answers
2






active

oldest

votes


















1














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






share|improve this answer























  • Now, I am getting this error: ActionController::UrlGenerationError in Metrics#index No route matches :action=>"edit", :controller=>"metrics", :id=>nil missing required keys: [:id]

    – Mallela SriPrakash
    Nov 13 '18 at 13:25












  • Your data variable is nil - try replacing it with the ID of the object that you want to edit

    – Mark
    Nov 13 '18 at 13:27











  • Remove get routes and then try.

    – t s
    Nov 13 '18 at 13:28











  • Yes, remove this line get 'edit' => 'metrics#edit'. That's already defined in resources :metrics better

    – Ursus
    Nov 13 '18 at 13:35






  • 1





    I definitely think that's the problem. If you have a different primary key than id, try this self.primary_key = '<your_column>'

    – Ursus
    Nov 13 '18 at 15:10



















0














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






share|improve this answer























  • This is what I have in my model: class Metric < ActiveRecord::Base self.table_name = 'AutomatedThresholding' end

    – Mallela SriPrakash
    Nov 13 '18 at 14:30











  • I guess you must have a database table named automated_thresholdings. If that's the case, this syntax self.table_name = AutomatedThresholding is wrong. This should be correct self.table_name = 'automated_thresholdings' Read more: apidock.com/rails/ActiveRecord/ModelSchema/ClassMethods/…

    – etoundi1er
    Nov 13 '18 at 18:31











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53281880%2ffacing-issues-with-link-to-edit-in-rails%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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






share|improve this answer























  • Now, I am getting this error: ActionController::UrlGenerationError in Metrics#index No route matches :action=>"edit", :controller=>"metrics", :id=>nil missing required keys: [:id]

    – Mallela SriPrakash
    Nov 13 '18 at 13:25












  • Your data variable is nil - try replacing it with the ID of the object that you want to edit

    – Mark
    Nov 13 '18 at 13:27











  • Remove get routes and then try.

    – t s
    Nov 13 '18 at 13:28











  • Yes, remove this line get 'edit' => 'metrics#edit'. That's already defined in resources :metrics better

    – Ursus
    Nov 13 '18 at 13:35






  • 1





    I definitely think that's the problem. If you have a different primary key than id, try this self.primary_key = '<your_column>'

    – Ursus
    Nov 13 '18 at 15:10
















1














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






share|improve this answer























  • Now, I am getting this error: ActionController::UrlGenerationError in Metrics#index No route matches :action=>"edit", :controller=>"metrics", :id=>nil missing required keys: [:id]

    – Mallela SriPrakash
    Nov 13 '18 at 13:25












  • Your data variable is nil - try replacing it with the ID of the object that you want to edit

    – Mark
    Nov 13 '18 at 13:27











  • Remove get routes and then try.

    – t s
    Nov 13 '18 at 13:28











  • Yes, remove this line get 'edit' => 'metrics#edit'. That's already defined in resources :metrics better

    – Ursus
    Nov 13 '18 at 13:35






  • 1





    I definitely think that's the problem. If you have a different primary key than id, try this self.primary_key = '<your_column>'

    – Ursus
    Nov 13 '18 at 15:10














1












1








1







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






share|improve this answer













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







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 13:20









UrsusUrsus

20.3k31430




20.3k31430












  • Now, I am getting this error: ActionController::UrlGenerationError in Metrics#index No route matches :action=>"edit", :controller=>"metrics", :id=>nil missing required keys: [:id]

    – Mallela SriPrakash
    Nov 13 '18 at 13:25












  • Your data variable is nil - try replacing it with the ID of the object that you want to edit

    – Mark
    Nov 13 '18 at 13:27











  • Remove get routes and then try.

    – t s
    Nov 13 '18 at 13:28











  • Yes, remove this line get 'edit' => 'metrics#edit'. That's already defined in resources :metrics better

    – Ursus
    Nov 13 '18 at 13:35






  • 1





    I definitely think that's the problem. If you have a different primary key than id, try this self.primary_key = '<your_column>'

    – Ursus
    Nov 13 '18 at 15:10


















  • Now, I am getting this error: ActionController::UrlGenerationError in Metrics#index No route matches :action=>"edit", :controller=>"metrics", :id=>nil missing required keys: [:id]

    – Mallela SriPrakash
    Nov 13 '18 at 13:25












  • Your data variable is nil - try replacing it with the ID of the object that you want to edit

    – Mark
    Nov 13 '18 at 13:27











  • Remove get routes and then try.

    – t s
    Nov 13 '18 at 13:28











  • Yes, remove this line get 'edit' => 'metrics#edit'. That's already defined in resources :metrics better

    – Ursus
    Nov 13 '18 at 13:35






  • 1





    I definitely think that's the problem. If you have a different primary key than id, try this self.primary_key = '<your_column>'

    – Ursus
    Nov 13 '18 at 15:10

















Now, I am getting this error: ActionController::UrlGenerationError in Metrics#index No route matches :action=>"edit", :controller=>"metrics", :id=>nil missing required keys: [:id]

– Mallela SriPrakash
Nov 13 '18 at 13:25






Now, I am getting this error: ActionController::UrlGenerationError in Metrics#index No route matches :action=>"edit", :controller=>"metrics", :id=>nil missing required keys: [:id]

– Mallela SriPrakash
Nov 13 '18 at 13:25














Your data variable is nil - try replacing it with the ID of the object that you want to edit

– Mark
Nov 13 '18 at 13:27





Your data variable is nil - try replacing it with the ID of the object that you want to edit

– Mark
Nov 13 '18 at 13:27













Remove get routes and then try.

– t s
Nov 13 '18 at 13:28





Remove get routes and then try.

– t s
Nov 13 '18 at 13:28













Yes, remove this line get 'edit' => 'metrics#edit'. That's already defined in resources :metrics better

– Ursus
Nov 13 '18 at 13:35





Yes, remove this line get 'edit' => 'metrics#edit'. That's already defined in resources :metrics better

– Ursus
Nov 13 '18 at 13:35




1




1





I definitely think that's the problem. If you have a different primary key than id, try this self.primary_key = '<your_column>'

– Ursus
Nov 13 '18 at 15:10






I definitely think that's the problem. If you have a different primary key than id, try this self.primary_key = '<your_column>'

– Ursus
Nov 13 '18 at 15:10














0














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






share|improve this answer























  • This is what I have in my model: class Metric < ActiveRecord::Base self.table_name = 'AutomatedThresholding' end

    – Mallela SriPrakash
    Nov 13 '18 at 14:30











  • I guess you must have a database table named automated_thresholdings. If that's the case, this syntax self.table_name = AutomatedThresholding is wrong. This should be correct self.table_name = 'automated_thresholdings' Read more: apidock.com/rails/ActiveRecord/ModelSchema/ClassMethods/…

    – etoundi1er
    Nov 13 '18 at 18:31
















0














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






share|improve this answer























  • This is what I have in my model: class Metric < ActiveRecord::Base self.table_name = 'AutomatedThresholding' end

    – Mallela SriPrakash
    Nov 13 '18 at 14:30











  • I guess you must have a database table named automated_thresholdings. If that's the case, this syntax self.table_name = AutomatedThresholding is wrong. This should be correct self.table_name = 'automated_thresholdings' Read more: apidock.com/rails/ActiveRecord/ModelSchema/ClassMethods/…

    – etoundi1er
    Nov 13 '18 at 18:31














0












0








0







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






share|improve this answer













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







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 14:08









etoundi1eretoundi1er

1315




1315












  • This is what I have in my model: class Metric < ActiveRecord::Base self.table_name = 'AutomatedThresholding' end

    – Mallela SriPrakash
    Nov 13 '18 at 14:30











  • I guess you must have a database table named automated_thresholdings. If that's the case, this syntax self.table_name = AutomatedThresholding is wrong. This should be correct self.table_name = 'automated_thresholdings' Read more: apidock.com/rails/ActiveRecord/ModelSchema/ClassMethods/…

    – etoundi1er
    Nov 13 '18 at 18:31


















  • This is what I have in my model: class Metric < ActiveRecord::Base self.table_name = 'AutomatedThresholding' end

    – Mallela SriPrakash
    Nov 13 '18 at 14:30











  • I guess you must have a database table named automated_thresholdings. If that's the case, this syntax self.table_name = AutomatedThresholding is wrong. This should be correct self.table_name = 'automated_thresholdings' Read more: apidock.com/rails/ActiveRecord/ModelSchema/ClassMethods/…

    – etoundi1er
    Nov 13 '18 at 18:31

















This is what I have in my model: class Metric < ActiveRecord::Base self.table_name = 'AutomatedThresholding' end

– Mallela SriPrakash
Nov 13 '18 at 14:30





This is what I have in my model: class Metric < ActiveRecord::Base self.table_name = 'AutomatedThresholding' end

– Mallela SriPrakash
Nov 13 '18 at 14:30













I guess you must have a database table named automated_thresholdings. If that's the case, this syntax self.table_name = AutomatedThresholding is wrong. This should be correct self.table_name = 'automated_thresholdings' Read more: apidock.com/rails/ActiveRecord/ModelSchema/ClassMethods/…

– etoundi1er
Nov 13 '18 at 18:31






I guess you must have a database table named automated_thresholdings. If that's the case, this syntax self.table_name = AutomatedThresholding is wrong. This should be correct self.table_name = 'automated_thresholdings' Read more: apidock.com/rails/ActiveRecord/ModelSchema/ClassMethods/…

– etoundi1er
Nov 13 '18 at 18:31


















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53281880%2ffacing-issues-with-link-to-edit-in-rails%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3