I have this really messy code in my update.js.erb file which resides in my video directory. It is called every time I add a new comment_title:
$(".comments_div").html('<% @video.comment_titles.each do |comment_title| %>
<div class ="comment_column_<%=
case @video.comment_titles.count
when 1
"wide"
when 2
"medium"
when 3
"narrow"
else
return
end
%>">
<% unless @video.comment_titles.count == 0 %>
<div id = "comment_title_<%= comment_title.id %>" class="comment_title">
<%= comment_title.title %>
<%= link_to "x", comment_title_path(comment_title.id), :method => :delete, :remote => true, :class => "comment_title_delete" %>
</div>
<% comment_title.comments.each do |comment| %>
<div class="comment_content">
<%= link_to image_tag(comment.user.profile.photo.url(:tiny)), profile_path(comment.user.profile), :class => "comment_image" %>
<div class="textual_comment_content">
<div class="comment_text">
<span class="name_link">
<%= link_to "#{comment.user.name}", profile_path(comment.user.profile), :class => "normal" %>
</span>
<%= comment.body.gsub("'",''') %>
</div>
<span class="comment_footer">
<ul>
<li class="list_style"><%= time_ago_in_words(comment.created_at) %> ago</li>
<% unless current_user != comment.user %>
<li><%= link_to "Delete", video_comment_path(:video_id => @video, :id => comment), :method => :delete, :class => "normal" %></li>
<% end %>
</ul>
</span>
</div>
</div>
<% end %>
</div>
<% end %>
<% end %>');
I also have a destroy.js.erb file which resides in my comment_title directory (because the destroy method is in the comment_titles controller) and is called whenever I delete a comment title. I want to have the same code as above in this destroy file so that when I delete a comment_title, the html code is updated. The problem is that @video
is not defined in my comment_title directory, so I cannot just copy and paste the code from update.js.erb to destroy.js.erb. So my question is what would be the best way to have the JS call above integrated into my destroy file?