开发者

How should I go about integrating AJAX?

开发者 https://www.devze.com 2023-02-22 09:51 出处:网络
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:

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 contro开发者_开发百科ller) 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号