Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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("'",'&apos;') %> 
              </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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
215 views
Welcome To Ask or Share your Answers For Others

1 Answer

You don't really need any javascript for destroy. When you click destroy you can just remove the div. and you won't need to update anything.

Well, actually you do need javascript but not through the controller, this can be done on the client side unless you really need to wait for a response.

Update

This assumes you are using REST.

Ruby array to loop through and identify comment destroy links

<% @video.comment_titles.each do |ct| %>
    <%= link_to "Destroy comment", ct, :method => :delete, :confirm => "Are you sure?", :class => 'destroy' %>
<% end %>

jQuery to process the destroy link:

$(document).ready(function() {
    $('a.destroy').live('click', function(event) {

        if ( confirm("Are you sure you want to delete this comment?") )
            $.ajax({
                url: this.href.replace('/delete', ''),
                type: 'post',
                dataType: 'script',
                data: { '_method': 'delete' },
                success: function() {
                    // the item has been deleted
                    // might want to remove it from the interface
                    // or redirect or reload by setting window.location
                }
            });
        return false;
    });
})

You comment controller:

def destroy
    @comment = Comment.find( params[:id] )
    @comment.destroy
    respond_to do |format|
        format.html { redirect_to :back }
        format.js { render :nothing => true }
    end
end

Let me know if you get any errors. I'm not quite sure about your routes so It's hard to guess everything.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...