# div\_for

**URL:** <https://discuss.rubyonrails.org/t/div-for/72569>\
**Category:** rubyonrails-talk\
**Created:** [June 16, 2017, 8:14pm UTC](https://discuss.rubyonrails.org/t/div-for/72569 "2017-06-16T20:14:00Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![fugee\_ohu](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/fugee_ohu/32/14370_2.png) [@fugee\_ohu](https://discuss.rubyonrails.org/u/fugee_ohu)\
**Post date:** [June 16, 2017, 8:14pm UTC](https://discuss.rubyonrails.org/t/div-for/72569/1 "2017-06-16T20:14:00Z")

</div>

The anchor doesn’t get rendered I didn’t know what to use for the element name so I used the same as I used the same variable I passed to div\_for in my view, commentable Thanks in advance

View:

\<%= div\_for(commentable) do %\>

\<% end %\>

js.erb

$(document).on(‘turbolinks:load’, function(){

$(‘#commentable’).each(function(){

var elm = $(this);

elm.append(‘[Comment](https://discuss.rubyonrails.org/comments/new?commentable_id='%20+%20elm.attr('id').split('_')%5B1%5D%20+%20')’);

```
  });

```

});

---

<div class="post-metadata">

**Author:** ![walterdavis](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/walterdavis/32/705_2.png) [@walterdavis](https://discuss.rubyonrails.org/u/walterdavis)\
**Post date:** [June 16, 2017, 10:47pm UTC](https://discuss.rubyonrails.org/t/div-for/72569/2 "2017-06-16T22:47:06Z")

</div>

There can only be one instance of an ID on any given page. And nothing you did in this example will render the id #commentable. If you look at my example, I did not use an ID on each element. The selector in $('#commentable').each should be $('#parent\_of\_the\_list div'), and you'll have to apply whatever ID you want to search for to a parent element of the commentables. If you want to do this in a slightly simpler manner, you could apply a classname to the commentables, using the div\_for helper: \<%= div\_for(commentable, class: 'commentable') do %\>. Then you can change the jQuery to $('.commentable') and that will also work.

Walter

---

<div class="post-metadata">

**Author:** ![fugee\_ohu](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/fugee_ohu/32/14370_2.png) [@fugee\_ohu](https://discuss.rubyonrails.org/u/fugee_ohu)\
**Post date:** [June 17, 2017, 7:53am UTC](https://discuss.rubyonrails.org/t/div-for/72569/3 "2017-06-17T07:53:06Z")

</div>

I’m render posts/index from a page called page.html.erb in the world controller and that in turn renders index action in posts controller and \_index partial which in turn renders \_post.html.erb The javascript anchor tag is in the top level view, page.html.erb The javascript has no effect on the html rendered No comment links displayed Thanks in advance

\_post.html.erb

\<%= div\_for(post, class: ‘post’) do %\>

\<%= simple\_format post.content %\>

\<% unless post.attachment.blank? %\>

\<%= image\_tag(post.attachment, height: 250) %\>

\<% end %\>

\<%- end -%\>

$(document).on(‘turbolinks:load’, function(){

$(‘#posts div’).each(function(){

var elm = $(this);

elm.append(‘[Comment](https://discuss.rubyonrails.org/comments/new?post_id='%20+%20elm.attr('id').split('_')%5B1%5D%20+%20')’);

});

});

---

<div class="post-metadata">

**Author:** ![fugee\_ohu](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/fugee_ohu/32/14370_2.png) [@fugee\_ohu](https://discuss.rubyonrails.org/u/fugee_ohu)\
**Post date:** [June 18, 2017, 11:58pm UTC](https://discuss.rubyonrails.org/t/div-for/72569/4 "2017-06-18T23:58:43Z")

</div>

Since I’m returning the new comment form shouldn’t it be rendered in a div that looks something like post\_14\_comment\_5 or something

---

<div class="post-metadata">

**Author:** ![fugee\_ohu](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/fugee_ohu/32/14370_2.png) [@fugee\_ohu](https://discuss.rubyonrails.org/u/fugee_ohu)\
**Post date:** [June 19, 2017, 12:05am UTC](https://discuss.rubyonrails.org/t/div-for/72569/5 "2017-06-19T00:05:23Z")

</div>

> There can only be one instance of an ID on any given page. And nothing you did in this example will render the id #commentable. If you look at my example, I did not use an ID on each element. The selector in $(‘#commentable’).each should be $(‘#parent\_of\_the\_list div’), and you’ll have to apply whatever ID you want to search for to a parent element of the commentables. If you want to do this in a slightly simpler manner, you could apply a classname to the commentables, using the div\_for helper: \<%= div\_for(commentable, class: ‘commentable’) do %\>. Then you can change the jQuery to $(‘.commentable’) and that will also work.
> 
> Walter
> 
> > The anchor doesn’t get rendered I didn’t know what to use for the element name so I used the same as I used the same variable I passed to div\_for in my view, commentable Thanks in advance
> 
> > View:
> 
> > \<%= div\_for(commentable) do %\> \<% end %\>
> 
> > js.erb
> 
> > $(document).on(‘turbolinks:load’, function(){ $(‘#commentable’).each(function(){ var elm = $(this); elm.append(‘[Comment](https://discuss.rubyonrails.org/comments/new?commentable_id=' + elm.attr('id').split('_')[1] + ')’); }); });

This isn’t working I think since it’s a link in the view i should use $('#post div).click.(function(){ but this isn’t rendering anything The log shows the js file was rendered but nothing was added to the page

---

<div class="post-metadata">

**Author:** ![fugee\_ohu](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/fugee_ohu/32/14370_2.png) [@fugee\_ohu](https://discuss.rubyonrails.org/u/fugee_ohu)\
**Post date:** [June 19, 2017, 12:24am UTC](https://discuss.rubyonrails.org/t/div-for/72569/6 "2017-06-19T00:24:02Z")

</div>

Here’s my page souce and js.erb it’s not appening anything to the page with the render

```ruby
<div class="post">
  <p>Doing nothing as usual</p>
  <a data-remote="true" href="/comments/new?comment%5Bcommentable_id%5D=14&amp;comment%5Bcommentable_type%5D=Post">Comment</a>
</div>
<div class="post" id="post_14">
</div>

$(document).on('turbolinks:load', function(){
  $('#post div').click.(function(){
    var elm = $(this);
    elm.append('<%= form_for @comment do |f| %> <%= f.hidden_field :commentable_id, value: @comment.commentable_id %><%= f.hidden_field :commentable_type, value: @comment.commentable_type %> <div class="field form-group"> <%= f.text_area :body, class: 'form-control' %> </div> <div class="field form-group"> <%= submit_tag "Post comment", class: 'btn btn-primary' %> </div>	<% end %>');
  });
});

```

---

<div class="post-metadata">

**Author:** ![walterdavis](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/walterdavis/32/705_2.png) [@walterdavis](https://discuss.rubyonrails.org/u/walterdavis)\
**Post date:** [June 19, 2017, 3:27am UTC](https://discuss.rubyonrails.org/t/div-for/72569/7 "2017-06-19T03:27:28Z")

</div>

Show me the content\_tag\_for code you are using in your view. Copy and paste. The id that will be rendered depends on the instance variable that you send into the helper. It doesn't care about the surrounding page or the route that it took to render it.

If you pass in \<%= content\_tag\_for :li, @foo do %\> then you will get \<li id="foo\_124"\>, no matter on which page you put this. (The controller for that page must set @foo to be an ActiveRecord object, but that's the only expectation.)

Walter

---

<div class="post-metadata">

**Author:** ![walterdavis](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/walterdavis/32/705_2.png) [@walterdavis](https://discuss.rubyonrails.org/u/walterdavis)\
**Post date:** [June 19, 2017, 3:30am UTC](https://discuss.rubyonrails.org/t/div-for/72569/8 "2017-06-19T03:30:11Z")

</div>

You've skipped over the last set of instructions I sent you, and you really need to do this in order or it will never make sense. Show me the code you are using that loads the form into another page, and please don't post it unless it is working in a browser and you can save comments. Get that far, and THEN I will be happy to help you take it into Ajax land.

Walter

---

<div class="post-metadata">

**Author:** ![fugee\_ohu](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/fugee_ohu/32/14370_2.png) [@fugee\_ohu](https://discuss.rubyonrails.org/u/fugee_ohu)\
**Post date:** [June 20, 2017, 7:29am UTC](https://discuss.rubyonrails.org/t/div-for/72569/9 "2017-06-20T07:29:20Z")

</div>

\_post.html.erb

\<%= content\_tag\_for(:div, post) do %\> \<%= simple\_format post.content %\> \<% unless post.attachment.blank? %\> \<%= image\_tag(post.attachment, height: 250) %\>  
\<% end %\> \<%= link\_to ‘Comment’, new\_comment\_path( ‘comment[commentable\_type]’: ‘Post’, ‘comment[commentable\_id]’: post.id), remote: true %\> \<%= content\_tag\_for(:div, post, :comment) do %\> \<% end %\> \<% end %\>

produces

```ruby
<div class="post" id="post_13">
	<p>listening to blues</p>
	<a data-remote="true" href="/comments/new?comment%5Bcommentable_id%5D=13&amp;comment%5Bcommentable_type%5D=Post">Comment</a>
	<div class="comment_post" id="comment_post_13">
</div></div><div class="post" id="post_14">
	<p>Doing nothing as usual</p>
	<a data-remote="true" href="/comments/new?comment%5Bcommentable_id%5D=14&amp;comment%5Bcommentable_type%5D=Post">Comment</a>
	<div class="comment_post" id="comment_post_14">
</div></div>

new.js.erb
  $('#post comment div)click.(function(){

This doesn't produce anything

```

---

<div class="post-metadata">

**Author:** ![walterdavis](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/walterdavis/32/705_2.png) [@walterdavis](https://discuss.rubyonrails.org/u/walterdavis)\
**Post date:** [June 20, 2017, 12:52pm UTC](https://discuss.rubyonrails.org/t/div-for/72569/10 "2017-06-20T12:52:39Z")

</div>

> \> \> \> \> There can only be one instance of an ID on any given page. And nothing you did in this example will render the id #commentable. If you look at my example, I did not use an ID on each element. The selector in $('#commentable').each should be $('#parent\_of\_the\_list div'), and you'll have to apply whatever ID you want to search for to a parent element of the commentables. If you want to do this in a slightly simpler manner, you could apply a classname to the commentables, using the div\_for helper: \<%= div\_for(commentable, class: 'commentable') do %\>. Then you can change the jQuery to $('.commentable') and that will also work. \> \> Walter \> \> \> \> \> The anchor doesn't get rendered I didn't know what to use for the element name so I used the same as I used the same variable I passed to div\_for in my view, commentable Thanks in advance \> \> \> \> \> \> View: \> \> \<%= div\_for(commentable) do %\> \> \> \<% end %\> \> \> \> \> js.erb \> \> $(document).on('turbolinks:load', function(){ \> \> $('#commentable').each(function(){ \> \> var elm = $(this); \> \> elm.append('\<a href="/comments/new?commentable\_id=' + elm.attr('id').split('\_')[1] + '"\>Comment\</a\>'); \> \> }); \> \> }); \> \> \> \> \> \> \> Here's my page souce and js.erb it's not appening anything to the page with the render \> \> \<div class="post"\> \> \<p\>Doing nothing as usual\</p\> \>  
> \> \> \<a data-remote="true" href="/comments/new?comment%5Bcommentable\_id%5D=14&amp;comment%5Bcommentable\_type%5D=Post"\>Comment\</a\> \> \</div\> \> \<div class="post" id="post\_14"\> \> \</div\> \> \> $(document).on('turbolinks:load', function(){ \> $('#post div').click.(function(){ \> var elm = $(this); \> elm.append('\<%= form\_for @comment do |f| %\> \<%= f.hidden\_field :commentable\_id, value: @comment.commentable\_id %\>\<%= f.hidden\_field :commentable\_type, value: @comment.commentable\_type %\> \<div class="field form-group"\> \<%= f.text\_area :body, class: 'form-control' %\> \</div\> \<div class="field form-group"\> \<%= submit\_tag "Post comment", class: 'btn btn-primary' %\> \</div\> \<% end %\>'); \> }); \> }); \>
> 
> You've skipped over the last set of instructions I sent you, and you really need to do this in order or it will never make sense. Show me the code you are using that loads the form into another page, and please don't post it unless it is working in a browser and you can save comments. Get that far, and THEN I will be happy to help you take it into Ajax land.
> 
> Walter
> 
> \_post.html.erb
> 
> \<%= content\_tag\_for(:div, post) do %\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= simple\_format post.content %\> &nbsp;&nbsp;&nbsp;&nbsp;\<% unless post.attachment.blank? %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= image\_tag(post.attachment, height: 250) %\>\<br\> &nbsp;&nbsp;&nbsp;&nbsp;\<% end %\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= link\_to 'Comment', new\_comment\_path( 'comment[commentable\_type]': 'Post', 'comment[commentable\_id]': post.id), remote: true %\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= content\_tag\_for(:div, post, :comment) do %\>

You may want to insert the existing comments here:

\<%= render post.comments %\>

Then you can inject the form at the top or the bottom of the list. Remember, you'll need a \_comment.html.erb partial in the views/comments folder for the short-hand to work.

> &nbsp;&nbsp;&nbsp;&nbsp;\<% end %\> \<% end %\>
> 
> produces
> 
> \<div class="post" id="post\_13"\> &nbsp;&nbsp;\<p\>listening to blues\</p\>
> 
> &nbsp;&nbsp;\<a data-remote="true" href="/comments/new?comment%5Bcommentable\_id%5D=13&amp;comment%5Bcommentable\_type%5D=Post"\>Comment\</a\> &nbsp;&nbsp;\<div class="comment\_post" id="comment\_post\_13"\> \</div\>\</div\>\<div class="post" id="post\_14"\> &nbsp;&nbsp;\<p\>Doing nothing as usual\</p\>
> 
> &nbsp;&nbsp;\<a data-remote="true" href="/comments/new?comment%5Bcommentable\_id%5D=14&amp;comment%5Bcommentable\_type%5D=Post"\>Comment\</a\> &nbsp;&nbsp;\<div class="comment\_post" id="comment\_post\_14"\> \</div\>\</div\>
> 
> new.js.erb &nbsp;&nbsp;$('#post comment div)click.(function(){
> 
> This doesn't produce anything

That's not surprising. Your RJS handler is going to be rendered with the same parameters that new.html.erb is when it creates a new page. So what you do in that instance is not observe a click (that click already happened) but rather identify the parts of the page that you want to replace, and replace them with the parts you need to insert. You're already on the other side of the click.

The content\_tag\_for method has a partner: dom\_id(element, name\_addition=nil). It produces just the ID for the element, not the entire element.

Inside the new.js.erb, rather than having the \<%= render 'form' %\> just spew out some HTML, you target where that should go:

$('#\<%= dom\_id(post, :comment) %\>').prepend('\<%=j render( 'comments/form' ) %\>');

That will expand to $('#comment\_post\_14').prepend('\<form ... \>'); which will inject the form into the top of the comment\_post\_N container.

Trouble is, it will also do that again and again if you click the link repeatedly. To get around that, you could put the link to add a comment inside the container you want to fill with the form, and then use the jQuery html() method to replace the contents. That would look the same as above, just replace prepend with html. This gets around you needing to test if the form is already there, because the moment you click the link, the link is replaced with the form and you can't click it again.

So you have something like this in the end:

\<%= content\_tag\_for(:div, post) do %\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= simple\_format post.content %\> &nbsp;&nbsp;&nbsp;&nbsp;\<% unless post.attachment.blank? %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= image\_tag(post.attachment, height: 250) %\>\<br\> &nbsp;&nbsp;&nbsp;&nbsp;\<% end %\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= content\_tag\_for(:div, post, :new\_comment) do %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= link\_to 'Comment', new\_comment\_path( 'comment[commentable\_type]': 'Post', 'comment[commentable\_id]': post.id), remote: true %\> &nbsp;&nbsp;&nbsp;&nbsp;\<% end %\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= content\_tag\_for(:div, post, :comments) do %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= render post.comments %\> &nbsp;&nbsp;&nbsp;&nbsp;\<% end %\> \<% end %\>

The new.js.erb becomes this:

$('#\<%= dom\_id(post, :new\_comment) %\>').html('\<%=j render( 'comments/form' ) %\>');

And then to update the page, you want to render just that post again into the list. This is why you wanted to have the posts/index.html.erb depend on a \_post.html.erb partial to render the list -- you're automatically set up to do this last step.

Make sure that your form\_for method includes the remote: true configuration pair. This will submit the form via Ajax.

In comments\_controller.rb, make sure you have a respond\_to block for :js in the #create method. In case of an error, it will need to render the :new method again, but if the save succeeds, it should just render the create.js.erb by default (pass an empty block to that content\_for).

create.js.erb would be something like this:

$('#\<%= dom\_id(@comment.post) %\>').replaceWith('\<%=j render @comment.post %\>');

And that's really it. If it doesn't work, it's probably a typo on my part.

Walter

---

<div class="post-metadata">

**Author:** ![fugee\_ohu](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/fugee_ohu/32/14370_2.png) [@fugee\_ohu](https://discuss.rubyonrails.org/u/fugee_ohu)\
**Post date:** [June 20, 2017, 6:57pm UTC](https://discuss.rubyonrails.org/t/div-for/72569/11 "2017-06-20T18:57:37Z")

</div>

In \_post.html.erb ?

---

<div class="post-metadata">

**Author:** ![walterdavis](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/walterdavis/32/705_2.png) [@walterdavis](https://discuss.rubyonrails.org/u/walterdavis)\
**Post date:** [June 20, 2017, 9:37pm UTC](https://discuss.rubyonrails.org/t/div-for/72569/12 "2017-06-20T21:37:11Z")

</div>

This part would go into \_post.html.erb

Walter

---

<div class="post-metadata">

**Author:** ![fugee\_ohu](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/fugee_ohu/32/14370_2.png) [@fugee\_ohu](https://discuss.rubyonrails.org/u/fugee_ohu)\
**Post date:** [June 21, 2017, 7:25am UTC](https://discuss.rubyonrails.org/t/div-for/72569/13 "2017-06-21T07:25:54Z")

</div>

List comments and inject the form at the top or bottom of the list, what about a link to new comment we skipped over that no?

---

<div class="post-metadata">

**Author:** ![fugee\_ohu](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/fugee_ohu/32/14370_2.png) [@fugee\_ohu](https://discuss.rubyonrails.org/u/fugee_ohu)\
**Post date:** [June 21, 2017, 7:52am UTC](https://discuss.rubyonrails.org/t/div-for/72569/14 "2017-06-21T07:52:42Z")

</div>

How do I give new.js.erb the right element name $('#comment\_post)click.(function(){ In this line there’s no id number

---

<div class="post-metadata">

**Author:** ![walterdavis](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/walterdavis/32/705_2.png) [@walterdavis](https://discuss.rubyonrails.org/u/walterdavis)\
**Post date:** [June 21, 2017, 12:33pm UTC](https://discuss.rubyonrails.org/t/div-for/72569/15 "2017-06-21T12:33:58Z")

</div>

I think you may have missed a few bits of my reply. We are not observing a click any more. Everything is out of the jQuery pool, and fully into the RJS pool. If you're viewing this in the Google Groups (Web) view, you may need to expand all of the little ... ellipses in order to see all of my threaded replies.

Walter

---

<div class="post-metadata">

**Author:** ![fugee\_ohu](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/fugee_ohu/32/14370_2.png) [@fugee\_ohu](https://discuss.rubyonrails.org/u/fugee_ohu)\
**Post date:** [June 27, 2017, 4:40am UTC](https://discuss.rubyonrails.org/t/div-for/72569/16 "2017-06-27T04:40:33Z")

</div>

in the second content\_tag\_for statement collection: post.comments fails to produce anthing for comment.body when the \_comment.html.erb partial is rendered

\_post.erb.html

\<%= content\_tag\_for(:div, post) do %\> \<%= simple\_format post.content %\> \<% unless post.attachment.blank? %\> \<%= image\_tag(post.attachment, height: 250) %\>  
\<% end %\> \<%= content\_tag\_for(:div, post, ‘comments’) do %\> \<%= render ‘/comments/comment’, collection: post.comments %\> \<%= content\_tag\_for(:div, post, ‘comment\_form\_holder’) do %\> \<%= link\_to ‘Comment’, new\_comment\_path(‘comment[commentable\_id]’: post.id, ‘comment[commentable\_type]’: ‘Post’) %\>  
  
\<% end %\> \<% end %\> \<% end %\>

\_comment.html.erb

\<%= content\_tag\_for(:div, @comments) do %\> \<%= simple\_format comment.body %\> \<%= comment.post.user.first\_name comment.post.user.last\_name %\> \<% end %\>

---

<div class="post-metadata">

**Author:** ![walterdavis](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/walterdavis/32/705_2.png) [@walterdavis](https://discuss.rubyonrails.org/u/walterdavis)\
**Post date:** [June 27, 2017, 11:32am UTC](https://discuss.rubyonrails.org/t/div-for/72569/17 "2017-06-27T11:32:12Z")

</div>

content\_tag\_for builds the DIV and gives it an ID based on a single item passed to it. I'm not sure what it would make of a collection of comments, which is what you're passing it here. What I think you need to do is change @comments to comment, to match the rest of the local variables inside the partial. @comments doesn't exist inside the partial unless you render it in a scope that already had that set as an instance variable. comment exists inside the partial because the partial is named comment, but that's only true if you are either rendering it with the "shortcut" collection: @comments, where @comments is an array or association of individual comment instances, or are rendering it one at a time, and passing in locals: { comment: @comment } or similar. Because you are doubly-nested at this point, inside \_post.html.erb, it is best to stick with the local variables that you have passed along, and not rely on the surrounding render context to magically provide variables for you.

Try just changing @comments to comment in the first line of your \_comment.html.erb, leave everything else alone, and see if it renders then. Just to be certain, check in the console whether you have any comments for that post you are trying to render. You should not see anything at all (the comment partial won't even render) if post.comments is empty.

Walter

---

<div class="post-metadata">

**Author:** ![fugee\_ohu](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/fugee_ohu/32/14370_2.png) [@fugee\_ohu](https://discuss.rubyonrails.org/u/fugee_ohu)\
**Post date:** [June 27, 2017, 1:12pm UTC](https://discuss.rubyonrails.org/t/div-for/72569/18 "2017-06-27T13:12:46Z")

</div>

I took out the div containers (content\_tag\_for) and just left: \<%= simple\_format comment.body %\> \<%= comment.post.user.first\_name comment.post.user.last\_name %\> but I still get the error that comment is undefined

---

<div class="post-metadata">

**Author:** ![walterdavis](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/walterdavis/32/705_2.png) [@walterdavis](https://discuss.rubyonrails.org/u/walterdavis)\
**Post date:** [June 28, 2017, 12:00am UTC](https://discuss.rubyonrails.org/t/div-for/72569/19 "2017-06-28T00:00:56Z")

</div>

> \> \> \> \> \> \> \> \> How do I give new.js.erb the right element name \> \> $('#comment\_post)click.(function(){ \> \> In this line there's no id number \> \> I think you may have missed a few bits of my reply. We are not observing a click any more. Everything is out of the jQuery pool, and fully into the RJS pool. If you're viewing this in the Google Groups (Web) view, you may need to expand all of the little ... ellipses in order to see all of my threaded replies. \> \> in the second content\_tag\_for statement collection: post.comments fails to produce anthing for comment.body when the \_comment.html.erb partial is rendered \> \> \_post.erb.html \> \> \<%= content\_tag\_for(:div, post) do %\> \> \<%= simple\_format post.content %\> \> \<% unless post.attachment.blank? %\> \> \<%= image\_tag(post.attachment, height: 250) %\>\<br\> \> \<% end %\> \> \<%= content\_tag\_for(:div, post, 'comments') do %\> \> \<%= render '/comments/comment', collection: post.comments %\> \> \<%= content\_tag\_for(:div, post, 'comment\_form\_holder') do %\> \> \<%= link\_to 'Comment', new\_comment\_path('comment[commentable\_id]': post.id, 'comment[commentable\_type]': 'Post') %\>\<br\>\<br\> \> \<% end %\> \> \<% end %\> \> \<% end %\> \> \> \> \_comment.html.erb \> \> \<%= content\_tag\_for(:div, @comments) do %\> \> \<%= simple\_format comment.body %\> \> \<%= comment.post.user.first\_name comment.post.user.last\_name %\> \> \<% end %\>
> 
> content\_tag\_for builds the DIV and gives it an ID based on a single item passed to it. I'm not sure what it would make of a collection of comments, which is what you're passing it here. What I think you need to do is change @comments to comment, to match the rest of the local variables inside the partial. @comments doesn't exist inside the partial unless you render it in a scope that already had that set as an instance variable. comment exists inside the partial because the partial is named comment, but that's only true if you are either rendering it with the "shortcut" collection: @comments, where @comments is an array or association of individual comment instances, or are rendering it one at a time, and passing in locals: { comment: @comment } or similar. Because you are doubly-nested at this point, inside \_post.html.erb, it is best to stick with the local variables that you have passed along, and not rely on the surrounding render context to magically provide variables for you.
> 
> Try just changing @comments to comment in the first line of your \_comment.html.erb, leave everything else alone, and see if it renders then. Just to be certain, check in the console whether you have any comments for that post you are trying to render. You should not see anything at all (the comment partial won't even render) if post.comments is empty.
> 
> Walter
> 
> I took out the div containers (content\_tag\_for) and just left: &nbsp;&nbsp;&nbsp;&nbsp;\<%= simple\_format comment.body %\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= comment.post.user.first\_name comment.post.user.last\_name %\> but I still get the error that comment is undefined

Where are you calling render on this template? Copy and paste the code here. Also copy and paste the error message so we can compare line numbers from the error with line numbers in your code.

Is the code that is quoted above exactly what you are using in your outer post partial? What should be happening if you call render 'comments/comment', collection: post.comments from inside the post.html.erb partial is that each of the post comments will be rendered one after another, and inside of each of those render calls, the instance variable 'comment' will be loaded with the one comment that is being rendered. This should Just Work™.

Walter

---

<div class="post-metadata">

**Author:** ![fugee\_ohu](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/fugee_ohu/32/14370_2.png) [@fugee\_ohu](https://discuss.rubyonrails.org/u/fugee_ohu)\
**Post date:** [June 28, 2017, 5:38am UTC](https://discuss.rubyonrails.org/t/div-for/72569/20 "2017-06-28T05:38:57Z")

</div>

After some reading it occurred to me explicitly calling the comment partial with \<%= render “comments/comment”, collection: post.comments %\> was defeating the convention for the variable as partial name so I replaced it with \<%= render @comments %\> and then it worked Of course I had to set @comments=post.comments first on the preceeding line I’d like to know how I could have gotten it working without treating @comments as a standalone resource (in the context of rendering partials) like that

[Next page](https://discuss.rubyonrails.org/t/div-for/72569.md?page=2)
