Cannot seem to display Comments on User Show Page

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

<%= form_for([@article, @article.comments.build]) do |f| %>
  <%= render 'shared/error_messages', :object => f.object %>
  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :content %><br />
    <%= f.text_area :content %>
  </div>
  <div class="actions">
    <%= f.submit "Post Comment" %>
  </div>
<% end %>

I am trying to follow this guide:
http://edgeguides.rubyonrails.org/getting_started.html#adding-a-second-model

Thanks.

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:

Comment Form:
<%= form_for([@article, @article.comments.build]) do |f| %>
  <%= render 'shared/error_messages', :object => f.object %>
  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :content %><br />
    <%= f.text_area :content %>
  </div>
  <div class="actions">
    <%= f.submit "Post Comment" %>
  </div>
<% end %>

shared/_comments.html.erb:
<% unless @comments.nil? || @comments.empty? %>
    <%= render :partial => 'shared/comment', :collection => @comments %>
  <%= will_paginate @comments %>
<% end %>

shared/_comment.html.erb
<%= comment.title %><br />
<%= comment.content %><br />
<%= comment.user.name %><br /><br />

Thank you.

You almost have it all correct, just in your view:

<%= form_for([@article, @comment]) do |f| %>

Fernando Perez wrote:

You almost have it all correct, just in your view:

<%= form_for([@article, @comment]) do |f| %>

--

http://digiprof.tv

That worked excellently.

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?

Kelp Kelp wrote:

Fernando Perez wrote:

You almost have it all correct, just in your view:

<%= form_for([@article, @comment]) do |f| %>

--

http://digiprof.tv

That worked excellently.

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.

But my edit link goes to:
http://localhost:3000/articles/320/comments/297/edit, when it should go
to http://localhost:3000/articles/297/comments/320/edit.

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

PsiPro wrote:

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.

Oops, I forgot to mention that I made some changes to the code in the
above posts. That's correct, though. I had to change it to
articles/show.
I still cannot figure out this problem:
My edit link(when using goes to:
http://localhost:3000/articles/320/comments/297/edit, when it should go
to http://localhost:3000/articles/297/comments/320/edit.

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

Maybe some confusion with the IDs?