div_for

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
28 messages Options
12
Reply | Threaded
Open this post in threaded view
|

div_for

fugee ohu
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>'); 
 }); 
}); 


--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/e775442d-f22a-4f45-b5ec-13b36ef61a8a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: div_for

Walter Lee Davis
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

> On Jun 16, 2017, at 4:14 PM, fugee ohu <[hidden email]> wrote:
>
> 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>');
>  });
> });
>
>

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/5AF15F80-CAA4-4873-AB7D-58F5544F7F6A%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: div_for

fugee ohu


On Friday, June 16, 2017 at 6:47:49 PM UTC-4, Walter Lee Davis wrote:
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

> On Jun 16, 2017, at 4:14 PM, fugee ohu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="ENrjst9uDQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> wrote:
>
> 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>');
>           });
> });
>
>


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) %><br>
    <% end %>
<%- end -%>

$(document).on('turbolinks:load', function(){ 
  $('#posts div').each(function(){ 
    var elm = $(this); 
    elm.append('<a href="/comments/new?post_id=' + elm.attr('id').split('_')[1] + '">Comment</a>'); 
  }); 
});  

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/dc689130-5195-43c0-84f9-c12d92956f84%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: div_for

fugee ohu
In reply to this post by Walter Lee Davis


On Friday, June 16, 2017 at 6:47:49 PM UTC-4, Walter Lee Davis wrote:
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

> On Jun 16, 2017, at 4:14 PM, fugee ohu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="ENrjst9uDQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> wrote:
>
> 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>');
>           });
> });
>
>



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

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/624e2860-96e6-421d-9089-c14df35e6f02%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: div_for

fugee ohu
In reply to this post by Walter Lee Davis


On Friday, June 16, 2017 at 6:47:49 PM UTC-4, Walter Lee Davis wrote:
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

> On Jun 16, 2017, at 4:14 PM, fugee ohu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="ENrjst9uDQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> wrote:
>
> 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>');
>           });
> });
>
>


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
 
$(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 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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/da0c5892-f53a-42fe-882c-3cd3f55a2f3f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: div_for

fugee ohu
In reply to this post by Walter Lee Davis


On Friday, June 16, 2017 at 6:47:49 PM UTC-4, Walter Lee Davis wrote:
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

> On Jun 16, 2017, at 4:14 PM, fugee ohu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="ENrjst9uDQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> wrote:
>
> 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 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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ac275ec2-5911-4692-9af2-f7f8fce055fa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: div_for

Walter Lee Davis
In reply to this post by fugee ohu

> On Jun 18, 2017, at 7:58 PM, fugee ohu <[hidden email]> wrote:
>
>
>
> On Friday, June 16, 2017 at 6:47:49 PM UTC-4, Walter Lee Davis wrote:
> 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
>
> > On Jun 16, 2017, at 4:14 PM, fugee ohu <[hidden email]> wrote:
> >
> > 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>');
> >           });
> > });
> >
> >
>
>
>
> 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

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

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/44DC27F9-48D6-4EB7-B4FD-895585C15715%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: div_for

Walter Lee Davis
In reply to this post by fugee ohu

> On Jun 18, 2017, at 8:24 PM, fugee ohu <[hidden email]> wrote:
>
>
>
> On Friday, June 16, 2017 at 6:47:49 PM UTC-4, Walter Lee Davis wrote:
> 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
>
> > On Jun 16, 2017, at 4:14 PM, fugee ohu <[hidden email]> wrote:
> >
> > 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

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/83E80992-1013-4ADC-B68B-E5822BE4788F%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: div_for

fugee ohu


On Sunday, June 18, 2017 at 11:30:27 PM UTC-4, Walter Lee Davis wrote:

> On Jun 18, 2017, at 8:24 PM, fugee ohu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="oYGIKnUbDgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> wrote:
>
>
>
> On Friday, June 16, 2017 at 6:47:49 PM UTC-4, Walter Lee Davis wrote:
> 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
>
> > On Jun 16, 2017, at 4:14 PM, fugee ohu <[hidden email]> wrote:
> >
> > 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 %>
    <%= simple_format post.content %>
    <% unless post.attachment.blank? %>
      <%= image_tag(post.attachment, height: 250) %><br>
    <% 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

<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


--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3fea1525-2b10-4718-9fa0-b6d04216aa51%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: div_for

Walter Lee Davis

> On Jun 20, 2017, at 3:29 AM, fugee ohu <[hidden email]> wrote:
>
>
>
> On Sunday, June 18, 2017 at 11:30:27 PM UTC-4, Walter Lee Davis wrote:
>
> > On Jun 18, 2017, at 8:24 PM, fugee ohu <[hidden email]> wrote:
> >
> >
> >
> > On Friday, June 16, 2017 at 6:47:49 PM UTC-4, Walter Lee Davis wrote:
> > 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
> >
> > > On Jun 16, 2017, at 4:14 PM, fugee ohu <[hidden email]> wrote:
> > >
> > > 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 %>
>     <%= simple_format post.content %>
>     <% unless post.attachment.blank? %>
>       <%= image_tag(post.attachment, height: 250) %><br>
>     <% 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 %>

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.

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

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 %>
    <%= simple_format post.content %>
    <% unless post.attachment.blank? %>
      <%= image_tag(post.attachment, height: 250) %><br>
    <% end %>
    <%= content_tag_for(:div, post, :new_comment) do %>
      <%= link_to 'Comment', new_comment_path( 'comment[commentable_type]': 'Post', 'comment[commentable_id]': post.id), remote: true %>
    <% end %>
    <%= content_tag_for(:div, post, :comments) do %>
      <%= render post.comments %>
    <% 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


>
>
>
> --
> 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 [hidden email].
> To post to this group, send email to [hidden email].
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3fea1525-2b10-4718-9fa0-b6d04216aa51%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/0AB50A72-3E18-438D-8991-15CB478DEDC9%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: div_for

fugee ohu


On Tuesday, June 20, 2017 at 8:52:58 AM UTC-4, Walter Lee Davis wrote:

> On Jun 20, 2017, at 3:29 AM, fugee ohu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="YeKoF7yIDgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> wrote:
>
>
>
> On Sunday, June 18, 2017 at 11:30:27 PM UTC-4, Walter Lee Davis wrote:
>
> > On Jun 18, 2017, at 8:24 PM, fugee ohu <[hidden email]> wrote:
> >
> >
> >
> > On Friday, June 16, 2017 at 6:47:49 PM UTC-4, Walter Lee Davis wrote:
> > 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
> >
> > > On Jun 16, 2017, at 4:14 PM, fugee ohu <[hidden email]> wrote:
> > >
> > > 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 %>
>     <%= simple_format post.content %>
>     <% unless post.attachment.blank? %>
>       <%= image_tag(post.attachment, height: 250) %><br>
>     <% end %>
>     <%= link_to 'Comment', new_comment_path( 'comment[commentable_type]': 'Post', 'comment[commentable_id]': <a href="http://post.id" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fpost.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNENbMwCHR_oS2BIpfisFlDNvniqcw&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fpost.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNENbMwCHR_oS2BIpfisFlDNvniqcw&#39;;return true;">post.id), remote: true %>
>     <%= 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.

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

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 %>
    <%= simple_format post.content %>
    <% unless post.attachment.blank? %>
      <%= image_tag(post.attachment, height: 250) %><br>
    <% end %>
    <%= content_tag_for(:div, post, :new_comment) do %>
      <%= link_to 'Comment', new_comment_path( 'comment[commentable_type]': 'Post', 'comment[commentable_id]': <a href="http://post.id" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fpost.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNENbMwCHR_oS2BIpfisFlDNvniqcw&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fpost.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNENbMwCHR_oS2BIpfisFlDNvniqcw&#39;;return true;">post.id), remote: true %>
    <% end %>
    <%= content_tag_for(:div, post, :comments) do %>
      <%= render post.comments %>
    <% 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


>
>
>
> --
> 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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="YeKoF7yIDgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="YeKoF7yIDgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
> To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/3fea1525-2b10-4718-9fa0-b6d04216aa51%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/3fea1525-2b10-4718-9fa0-b6d04216aa51%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/3fea1525-2b10-4718-9fa0-b6d04216aa51%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/3fea1525-2b10-4718-9fa0-b6d04216aa51%40googlegroups.com.
> For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

In _post.html.erb ?

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/35c41e75-38a8-4995-8ece-9e54ee13c530%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: div_for

Walter Lee Davis

> On Jun 20, 2017, at 2:57 PM, fugee ohu <[hidden email]> wrote:
>
> <%= 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, :new_comment) do %>
>       <%= link_to 'Comment', new_comment_path( 'comment[commentable_type]': 'Post', 'comment[commentable_id]': post.id), remote: true %>
>     <% end %>
>     <%= content_tag_for(:div, post, :comments) do %>
>       <%= render post.comments %>
>     <% end %>
> <% end %>


This part would go into _post.html.erb

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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/16432F1F-E312-44BE-9577-23E9D6A253EB%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: div_for

fugee ohu
In reply to this post by Walter Lee Davis


On Tuesday, June 20, 2017 at 8:52:58 AM UTC-4, Walter Lee Davis wrote:

> On Jun 20, 2017, at 3:29 AM, fugee ohu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="YeKoF7yIDgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> wrote:
>
>
>
> On Sunday, June 18, 2017 at 11:30:27 PM UTC-4, Walter Lee Davis wrote:
>
> > On Jun 18, 2017, at 8:24 PM, fugee ohu <[hidden email]> wrote:
> >
> >
> >
> > On Friday, June 16, 2017 at 6:47:49 PM UTC-4, Walter Lee Davis wrote:
> > 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
> >
> > > On Jun 16, 2017, at 4:14 PM, fugee ohu <[hidden email]> wrote:
> > >
> > > 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 %>
>     <%= simple_format post.content %>
>     <% unless post.attachment.blank? %>
>       <%= image_tag(post.attachment, height: 250) %><br>
>     <% end %>
>     <%= link_to 'Comment', new_comment_path( 'comment[commentable_type]': 'Post', 'comment[commentable_id]': <a href="http://post.id" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fpost.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNENbMwCHR_oS2BIpfisFlDNvniqcw&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fpost.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNENbMwCHR_oS2BIpfisFlDNvniqcw&#39;;return true;">post.id), remote: true %>
>     <%= 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.

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

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 %>
    <%= simple_format post.content %>
    <% unless post.attachment.blank? %>
      <%= image_tag(post.attachment, height: 250) %><br>
    <% end %>
    <%= content_tag_for(:div, post, :new_comment) do %>
      <%= link_to 'Comment', new_comment_path( 'comment[commentable_type]': 'Post', 'comment[commentable_id]': <a href="http://post.id" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fpost.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNENbMwCHR_oS2BIpfisFlDNvniqcw&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fpost.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNENbMwCHR_oS2BIpfisFlDNvniqcw&#39;;return true;">post.id), remote: true %>
    <% end %>
    <%= content_tag_for(:div, post, :comments) do %>
      <%= render post.comments %>
    <% 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


>
>
>
> --
> 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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="YeKoF7yIDgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="YeKoF7yIDgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
> To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/3fea1525-2b10-4718-9fa0-b6d04216aa51%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/3fea1525-2b10-4718-9fa0-b6d04216aa51%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/3fea1525-2b10-4718-9fa0-b6d04216aa51%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/3fea1525-2b10-4718-9fa0-b6d04216aa51%40googlegroups.com.
> For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.


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?

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/6e4181c9-5e44-40f4-ab04-361a8f0906c2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: div_for

fugee ohu
In reply to this post by Walter Lee Davis


On Tuesday, June 20, 2017 at 8:52:58 AM UTC-4, Walter Lee Davis wrote:

> On Jun 20, 2017, at 3:29 AM, fugee ohu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="YeKoF7yIDgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> wrote:
>
>
>
> On Sunday, June 18, 2017 at 11:30:27 PM UTC-4, Walter Lee Davis wrote:
>
> > On Jun 18, 2017, at 8:24 PM, fugee ohu <[hidden email]> wrote:
> >
> >
> >
> > On Friday, June 16, 2017 at 6:47:49 PM UTC-4, Walter Lee Davis wrote:
> > 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
> >
> > > On Jun 16, 2017, at 4:14 PM, fugee ohu <[hidden email]> wrote:
> > >
> > > 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 %>
>     <%= simple_format post.content %>
>     <% unless post.attachment.blank? %>
>       <%= image_tag(post.attachment, height: 250) %><br>
>     <% end %>
>     <%= link_to 'Comment', new_comment_path( 'comment[commentable_type]': 'Post', 'comment[commentable_id]': <a href="http://post.id" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fpost.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNENbMwCHR_oS2BIpfisFlDNvniqcw&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fpost.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNENbMwCHR_oS2BIpfisFlDNvniqcw&#39;;return true;">post.id), remote: true %>
>     <%= 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.

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

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 %>
    <%= simple_format post.content %>
    <% unless post.attachment.blank? %>
      <%= image_tag(post.attachment, height: 250) %><br>
    <% end %>
    <%= content_tag_for(:div, post, :new_comment) do %>
      <%= link_to 'Comment', new_comment_path( 'comment[commentable_type]': 'Post', 'comment[commentable_id]': <a href="http://post.id" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fpost.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNENbMwCHR_oS2BIpfisFlDNvniqcw&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fpost.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNENbMwCHR_oS2BIpfisFlDNvniqcw&#39;;return true;">post.id), remote: true %>
    <% end %>
    <%= content_tag_for(:div, post, :comments) do %>
      <%= render post.comments %>
    <% 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


>
>
>
> --
> 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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="YeKoF7yIDgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="YeKoF7yIDgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
> To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/3fea1525-2b10-4718-9fa0-b6d04216aa51%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/3fea1525-2b10-4718-9fa0-b6d04216aa51%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/3fea1525-2b10-4718-9fa0-b6d04216aa51%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/3fea1525-2b10-4718-9fa0-b6d04216aa51%40googlegroups.com.
> For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.


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

 

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/86014d0a-3224-497e-a78c-4866e879ac0f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: div_for

Walter Lee Davis

> On Jun 21, 2017, at 3:52 AM, fugee ohu <[hidden email]> 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.

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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/C456D1E6-2B3A-4316-AD3A-CC3A3055EBB3%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: div_for

fugee ohu


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 <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="nWFgNt9LDgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> 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.

Walter

I started out with an example posted only how to use acts_as_commentable_with_threading gem by Dustin Fisher First thing is he passes the object comentable from controller to view it's not prepended by an @, I didn't know you could do that That aside, his comments controller has no new action only create https://github.com/DustinFisher/acts-as-commentable-with-threading-example/blob/master/app/controllers/comments_controller.rb Can you please look at it and clarify for me Thanks

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/727c7067-cbfe-4465-b140-312eb6414bc8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: div_for

fugee ohu
In reply to this post by Walter Lee Davis


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 <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="nWFgNt9LDgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> 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.

Walter


In the dustin fisher example  https://github.com/DustinFisher/acts-as-commentable-with-threading-example he doesn't have a new action in the comments_controller.rb Why not?

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/cf692441-3af1-4826-bf1e-ab4359d94be0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: div_for

fugee ohu
In reply to this post by Walter Lee Davis


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 <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="nWFgNt9LDgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> 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 %>

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ed82e3fd-acfe-4cfd-8ccf-f2fd95c2f76a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: div_for

Walter Lee Davis

> On Jun 27, 2017, at 12:40 AM, fugee ohu <[hidden email]> 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 <[hidden email]> 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

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/B04B0648-0933-45A9-9B5A-BAB609792F2E%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: div_for

fugee ohu


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 <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="yQluJCTwAAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> 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 <[hidden email]> 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]': <a href="http://post.id" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fpost.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNENbMwCHR_oS2BIpfisFlDNvniqcw&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fpost.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNENbMwCHR_oS2BIpfisFlDNvniqcw&#39;;return true;">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

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.
12