The action 'show' could not be found for CommentsController - Ruby Tutorial

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


The action 'show' could not be found for CommentsController - Ruby Tutorial



I am trying to follow the following guide for starting Ruby on Rails. Everything went well until I try to destroy (delete comments).



I receive the following error:
The action 'show' could not be found for CommentsController



I will post my code below.



http://guides.rubyonrails.org/getting_started.html



comments_controller.rb


class CommentsController < ApplicationController

http_basic_authenticate_with name: "dhh", password: "secret", only: :destroy

def create
@article = Article.find(params[:article_id])
@comment = @article.comments.create(comment_params)
redirect_to article_path(@article)
end

def destroy
@article = Article.find(params[:article_id])
@comment = @article.comments.find(params[:id])
@comment.destroy
redirect_to article_path(@article)
end

private
def comment_params
params.require(:comment).permit(:commenter, :body)
end

end



articles_controller


class ArticlesController < ApplicationController

http_basic_authenticate_with name: "dhh", password: "secret", except: [:index, :show]

def index
@articles = Article.all
end

def show
@article = Article.find(params[:id])
end

def new
@article = Article.new
end

def edit
@article = Article.find(params[:id])
end

def create
@article = Article.new(article_params)

if @article.save
redirect_to @article
else
render 'new'
end
end

def update
@article = Article.find(params[:id])

if @article.update(article_params)
redirect_to @article
else
render 'edit'
end
end

def destroy
@article = Article.find(params[:id])
@article.destroy

redirect_to articles_path
end

private
def article_params
params.require(:article).permit(:title, :text)
end
end



routes.rb


Rails.application.routes.draw do

resources :articles do
resources :comments
end

root 'welcome#index'
end



article.rb


class Article < ActiveRecord::Base

has_many :comments, dependent: :destroy

validates :title, presence: true,
length: { minimum: 5}
end



comment.rb


class Comment < ActiveRecord::Base
belongs_to :article
end



articles/index.html.erb


<h1>Listing Articles</h1>

<%= link_to 'New article', new_article_path %>

<table>
<tr>
<th>Title</th>
<th>Text</th>
<th colspan="3"></th>
</tr>

<% @articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td><%= link_to 'Show', article_path(article) %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>



Just not sure what it can be. Please advise. I will supply more code if needed.





can you past in your comment and article model please
– Cyzanfar
Jan 15 '16 at 18:32





Just posted the models sir. Thanks!
– FBaez51
Jan 15 '16 at 21:21





Please post the URL that gives you the error.
– AnoE
Jan 15 '16 at 21:24





localhost:3000/articles/2/comments/1 is the link giving me the message.
– FBaez51
Jan 15 '16 at 21:41







Why did I get a -1? Anyways..still working on this! Appreciate any help!
– FBaez51
Jan 15 '16 at 21:51




4 Answers
4



The answer will be in your routes.rb and in the URL you are trying to call.



The URL is probably /comment/123, and the routes.rb likely has something like get 'comment/:id' => 'comment#show' in it (or a resource statement for comments).


/comment/123


get 'comment/:id' => 'comment#show'


resource



So rails tries to show the page for a comment and you did not implement the function. The error message is saying exactly what it needs to say (you do indeed not have a .show method in your CommentsController).


.show


CommentsController



If my guesses were not correct, then please post your routes.rb and the URL you are requesting.





might be refreshing the page, which i personally did a lot of time. :)
– Abhinay
Jan 15 '16 at 18:53





I am using resources
– FBaez51
Jan 19 '16 at 14:17



You can add


def show
@article = Article.find(params[:article_id])
@comment = @article.comments.find(params[:id])
@comment.destroy
redirect_to article_path(@article)
end



to your comments_controller.rb


comments_controller.rb



Actually these codes are identical to those in the def destroy


def destroy



I've been working through the same tutorial, and just today ran into the exact same issue as the original poster.



I didn't notice exactly when it started, but it seemed to have started somewhere in section 6, 7 or 8 (adding comments to blog, refactoring, or adding deletion of comments).



Furthermore, clicking links within the app (i.e. the 'Back' link) would spawn a new tab. Likewise, when clicking the 'Destroy' link, the browser's confirmation dialog would appear over the current tab (correct behavior), but at the same time a new browser tab would appear (incorrect) - I think this new tab behavior (a type of 'show'ing of the comment that the user is in process of deciding whether to go ahead and destroy) is what was causing the error.



After finishing the tutorial (one more section, basic authentication), I quit & re-launched Chrome, and then also quit & restarted the server (ctrl-c, then bin/rails server).



After doing this, all is well. Both the spawning extra tab behavior, as well as the spurious show action being triggered (thus causing the error) - when the destroy confirmation sheet appears - have stopped.



So it seems that this problem was simply some spurious noise - perhaps caused by something that's changed (in the rails environment?) between the time the tutorial was originally written, and when the original poster ran into this behavior (and still persisting today, July '18, with rails 5.2).



Update: As I mentioned earlier you dont have show method in your CommentsController so you wont be able to visit the show page for any comment.


show


CommentsController


show


comment



As you said you are trying to visit this url: localhost:3000/articles/2/comments/1 which is the show page for comment having id: 1 and the routes(resources :comments) is trying to take it to show action. Hence the error rightly thrown show could not be found for CommentsController.


localhost:3000/articles/2/comments/1


comment


id:


resources :comments


show


show could not be found for CommentsController



Please remove the url from your browser and try visiting again the article show page. It will work.


article


show





So I just put a empty show in there? Example: def show end?
– FBaez51
Jan 15 '16 at 18:24







Negative, that didnt work :(
– FBaez51
Jan 15 '16 at 18:26





Nopes, putting show in your controller wont work as its looking for show method in wrong controller.
– Abhinay
Jan 15 '16 at 18:27





@Abhinay your answer is incorrect and doesn't provide any code...
– Cyzanfar
Jan 15 '16 at 18:30





@Cyzanfar yeah I am looking at it..its quite surprising that the application is still looking for show method in CommentsController even after redirect to article_path(@article)
– Abhinay
Jan 15 '16 at 18:34




show


CommentsController


article_path(@article)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

5dGCCgrrJRMhzO,mg,hL,nfZGdbbd43O22j,kqS tG,LIWfcONVqUHexRY
otkUxkZxMQZ 1YE7sBtNI Z Z3ZE9r8kf2Wz N6Ivs xRq4fuZ

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

Visual Studio Code: How to configure includePath for better IntelliSense results