Hello,
I have added a comment form to my article show page, but after I submit
the comment, I get the following error: Couldn't find Article without an
ID.
I think it is because the Comments controller does not know which
article to add the new comment.
comments_controller:
def create
@article = Article.find(params[:article_id])
@comment = @article.comments.create(params[:comment])
if @comment.save
flash[:success] = "Comment saved."
redirect_to articles_path(@article)
else
render 'show'
end
end
article show page:
<% @article.comments.each do |comment| %>
<%= comment.title %>
<%= comment.content %>
<%= comment.user %>
<% end %>
It would help to know the URL your being redirected to... but I see
one potential issue:
if @comment.save
flash[:success] = "Comment saved."
redirect_to articles_path(@article)
else
render 'show'
end
Lets pretened your comment failed to save... your rendering show...
Show what? By default your going to render the comment#show action
with no ID. You want to be rendering articles/show i assume.
I figured out the problem with the original post, but I have a new
problem.
When a user submits an invalid comment(if the title or content does not
validate correctly), they get sent back to the article. There are two
problems, though.
1. The form does not keep the previous field content, so the form
resets.
2. All the comments disappear.
Articles controller:
def show
@article = Article.find(params[:id])
@comments = @article.comments.paginate(:page => params[:page])
@title = @article.title
end
Comments controller:
def create
@article = Article.find(params[:article_id])
@comment = @article.comments.build(params[:comment])
@comment.user_id = current_user.id
if @comment.save
flash[:success] = "Comment saved."
redirect_to @article
else
flash[:error] = "Error in creating comment."
render 'articles/show'
end
end
Here's the comment form and the comment display code, if you need it:
I have one more question about the same set of files. I want to add a
link for editing the comments on the article show page, but I am getting
an error.
This is the code I am using to display the edit comment link:
<%= link_to "Edit Comment", edit_comment_path(comment) %>
Here is the error I get:
undefined method `edit_comment_path' for #<#<Class:0xa2f2c90>:0xa2f04f4>
I think the reason I get the error is that I am using the code in the
article show page, which is in the article controller, and so there is
no predefined path to 'edit_comment_path'. Is there a way to access the
path inside of article and send the object 'comment' to it?
I have one more question about the same set of files. I want to add a
link for editing the comments on the article show page, but I am getting
an error.
This is the code I am using to display the edit comment link:
<%= link_to "Edit Comment", edit_comment_path(comment) %>
Here is the error I get:
undefined method `edit_comment_path' for #<#<Class:0xa2f2c90>:0xa2f04f4>
I think the reason I get the error is that I am using the code in the
article show page, which is in the article controller, and so there is
no predefined path to 'edit_comment_path'. Is there a way to access the
path inside of article and send the object 'comment' to it?
Okay, I found out that I should be using
edit_article_comment_path(comment) to compensate for the comment
resource being inside the article resource.
The ID's are reversed for some reason. Here is the edit method of the
comments controller:
def edit
@comment = Comment.find(params[:id])
@article = Article.find(params[:article_id])
@title = "Edit Comment"
end
It would help to know the URL your being redirected to... but I see
one potential issue:
if @comment.save
flash[:success] = "Comment saved."
redirect_to articles_path(@article)
else
render 'show'
end
Lets pretened your comment failed to save... your rendering show...
Show what? By default your going to render the comment#show action
with no ID. You want to be rendering articles/show i assume.
Is there a problem with my articles show page:
def show
@article = Article.find(params[:id])
@comments = @article.comments.paginate(:page => params[:page])
@comment = Comment.new
@title = @article.title
end