Made a comment system that shows/hides by a JS file but at the same time this comment system is controlled by the same button that is used as a view counter. Both are run by two different Ajax files but both won’t work without the other one not working. Any help will be much appreciated! Thank you!
EDIT the HTML isn’t using @micropost and using micropost because @micropost is defined in a different partial. For those who are familiar to Michael Hartl’s Rails Tutorial, I have done this micropost with his code and teachings. I know this is screwing up everything for the ajax which is bringing back a @micropost rather than micropost but it won’t let me not put a @ in the ajax file so I am lost
Comment Button HTML
<span class='count-<%= micropost.id%>'>
<a href="/microposts/<%=micropost.id %>/upview" data-remote='true' class='CommentTitle' data-micropost='<%= micropost.id %>'>Comments</a>
</span>
Comment Ajax for changing counter
$("#count-<%=@micropost.id%>").html('<%="#{@micropost.view_count}"%>');
$(".count-<%=@micropost.id%>").html('<a href="/microposts/<%=@micropost.id%>/upview" data-remote="true" class="CommentTitle" data-micropost="<%= @micropost.id %>">Comments</a>');
Comment Button Slide Toggle JS
$(".CommentTitle").click(function(){
var title = this;
var postID = $(this).data('micropost');
$("#CommentContainer-" + postID).slideToggle("slow", function(){
$(".CommentTitle", title).html($(this).is(":hidden") ? "Comments" : "Comments");
});
});
Assumption: When the user clicks on the link in the html, they’re sending a request to your
upviewaction inside yourMicropostsController.Inside that controller action method, you can assign an instance variable…
then, inside your
upview.js.erb, you can use that instance variable for whatever you want.Hope this helps!