Ruby on Rails Saturday, June 17, 2017



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 <fuge...@gmail.com> 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 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/dc689130-5195-43c0-84f9-c12d92956f84%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment