I have successfully integrated comments into public activity following a tutorial on public activity #406 Public Activity. However, I am facing issues with submitting the comments via ajax. I have gone through several tutorials but haven't been able to resolve the issue. Here is an excerpt of my code:
This is how the comments controller looks like:
def create
@activity = Activity.find(params[:activity_id])
@comment = @activity.comments.create!(comment_params)
@comment.user = current_user
@users= User.joins(:comments).where(talks: {id: @activity.talk_ids}).push(@comment.user).reject {|user| user == @comment.user }.uniq
@users.each do |user|
@comment.create_activity :create, owner: user
end
respond_to do |format|
format.html { redirect_to user_path(current_user), notice: 'Comment created.' }
format.js
end
end
This is a snippet of the partial I created for each activity:
<div class="media social-box">
<a class="pull-left social-users-avatars" href="#">
<%= link_to image_tag(activity.user.image.url(:small)) ,activity.user%>
<p><%= link_to activity.user.username, activity.user if feed.activity %>
<span style="font-size: 11px; color:#4edee1;">Added this item </span> </p>
</a>
<ul class="unstyled custumer_say">
<li class="clearfix" style="list-style: none">
<%= link_to image_tag(sell.image.url,:style=> "width: 40%; padding-right: 10px;", :class=> "pull-left img_client"), sell%>
<div class="entry-content">
<header>
<span class="entry-date">— <%="#{time_ago_in_words(sell.created_at)} ago "%> </span>
</header>
</div>
</li>
</ul>
<div class="media-body social-body">
<div class="social-footer">
<div class="social-info-users">
<strong><%= pluralize(activity.comments.size, "Comment") %></strong>
</div>
<div class="social-comments">
<ul id="chat">
<%= render activity.comments %>
</ul>
<% if current_user.friends.present? %>
<div class="media">
<a class="pull-left" href="#">
<%= image_tag(current_user.image.url(:tiny),:style=> "width: 100%;")%>
</a>
<div class="media-body">
<%= form_for([activity, activity.comments.build],:remote => true) do |f| %>
<%= f.text_field :details, :class=>"input-block-level", :placeholder=>"write a comment" %>
<% end%>
</div>
</div>
<% end %>
</div>
</div>
</div>
</div>
I want to update the CSS id as follows:
<ul id="chat">
<%= render activity.comments %>
</ul>
The content of my create.js.erb file is:
$("#chat").append('<%= j render(activity.comments) %>');
$("#new_comment")[0].reset();
It's worth noting that my partial is rendered as
<%= render activity.comments %>
instead of the more common <%= render @activity.comments %>
, which works correctly. The entire code is displayed on the showpage of the current user (e.g., users/1 page).