# Problem with associating comments with blog posts on same page

**URL:** <https://www.ruby-forum.com/t/problem-with-associating-comments-with-blog-posts-on-same-page/181130>\
**Category:** Rails\
**Created:** [January 6, 2010, 1:00am UTC](https://www.ruby-forum.com/t/problem-with-associating-comments-with-blog-posts-on-same-page/181130 "2010-01-06T01:00:53Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdgriffith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/edb3f5/32.png) [@jdgriffith](https://www.ruby-forum.com/u/jdgriffith)\
**Post date:** [January 6, 2010, 1:00am UTC](https://www.ruby-forum.com/t/problem-with-associating-comments-with-blog-posts-on-same-page/181130/1 "2010-01-06T01:00:53Z")

</div>

Hi,

I will admit I am very new to ruby and I am trying to do something  
that is not in any tutorials I have found. If you see a more  
conventional way to do some of the things I am trying to do, please  
let me know.

What I am trying to accomplish. People can edit the comments on the  
same page as the listing of posts. The problem is that when I go to  
create the comment, it is not getting associated with the post. I have  
accomplished this on other pages through normal methods such as:

app/views/posts/show.html.erb  
\<% form\_for [@post, Comment.new] do |f| %\>

This successfully pulls the post id and saves the comment as belonging  
to that post.

Now, when I try to incorporate everything all into a very easy page to  
navigate I can’t use the same code associations and my comments fail.

app/views/posts/index.html.erb  
\<% div\_for( post ) do %\>

### \<%= link\_to\_unless\_current h(post.title), statemant %\>

Posted \<%=h time\_ago\_in\_words(post.created\_at) %\> ago

Approved: \<%= h(post.approved) %\>

\<%= simple\_format h(post.body) %\>

\<% randomNumber = rand(100000) %\> ![](https://test.ruby-forum.com/images/add_comment.jpg)
 \<%= render :partial =\> post.comments %\> 
# New comment
\<% form\_for :comment, :url =\> { :controller =\> "comments", :action =\> :create} do |f| %\> \<%= f.error\_messages %\> \<%= f.hidden\_field(:post\_id, :value =\> post.post\_id) %\> 

\<%= f.text\_area :body %\>

\<%= f.submit 'Add your Comment' %\>

\<% end %\> 
\<% end %\>

Thanks for any help or pointers. I hope that you can understand what I  
am trying to accomplish.

Justin
