Kellogs
Kellogs

Reputation: 471

Rails: Two different Ajax features not working together on same button

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-<%[email protected]%>").html('<%="#{@micropost.view_count}"%>');
$(".count-<%[email protected]%>").html('<a href="/microposts/<%[email protected]%>/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");  
}); 
});

Upvotes: 1

Views: 157

Answers (1)

Fishz
Fishz

Reputation: 465

Assumption: When the user clicks on the link in the html, they're sending a request to your upview action inside your MicropostsController.

Inside that controller action method, you can assign an instance variable...

@this_micropost = Micropost.find(params[:id]) # assuming you're RESTful, which it looks like

then, inside your upview.js.erb, you can use that instance variable for whatever you want.

Hope this helps!

Upvotes: 1

Related Questions