Ruby on Rails Tuesday, June 27, 2017



On Tuesday, June 27, 2017 at 8:01:40 PM UTC-4, Walter Lee Davis wrote:

> On Jun 27, 2017, at 9:12 AM, fugee ohu <fuge...@gmail.com> wrote:
>
>
>
> On Tuesday, June 27, 2017 at 7:32:33 AM UTC-4, Walter Lee Davis wrote:
>
> > On Jun 27, 2017, at 12:40 AM, fugee ohu <fuge...@gmail.com> wrote:
> >
> >
> >
> > On Wednesday, June 21, 2017 at 8:34:17 AM UTC-4, Walter Lee Davis wrote:
> >
> > > On Jun 21, 2017, at 3:52 AM, fugee ohu <fuge...@gmail.com> wrote:
> > >
> > > 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:
>     <%= simple_format comment.body %>
>     <%= 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

>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/eea548b0-8979-4f0c-9662-9c720f5f35db%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.


Thanks for staying with me on this Here's the trace goes Users page where people visit users they're following or searched for This is the page action in the world controller and identifies users by their id which is part of the url This page then renders the index action of the  posts controller to the partial _index instead of just plain index.html.erb which in turn uses   <%= render @posts %> to render _post.html.erb Here's my _post.html.erb and _comment.html.erb again

_post.html.erb
<%= 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

    <%= simple_format comment.body %>
    <%= comment.post.user.first_name comment.post.user.last_name %>




--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/036f3182-d385-4cb3-bbf0-e8f47b1386f9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment