mardi 27 octobre 2015

How to reload current page when destroy a micropost with AJAX in RAILS

guys! I'm start to learn RAILS. I have a list of micropost using pagination. And when I destroy a micropost, it go to first page. But I want when I destroy a micropost, it will reload a current page.

This is my code:

static_pages_controller.rb

def home
    return unless logged_in?
    @micropost  = current_user.microposts.build
    @feed_items = current_user.feed.paginate(page: params[:page])
end

microposts_controller.rb

def destroy
    @micropost.destroy
    @feed_items = current_user.feed.paginate(page: params[:page])
    respond_to do |format|
      format.html { redirect_to request.referrer || root_url }
      format.js
    end
  end

destroy.js.erb

$("#microposts").html("<%= escape_javascript(render('shared/feed')) %>");

_microposts.html.erb

<% if current_user?(micropost.user) %>
      <%= link_to "Delete", micropost, remote: true,
                                       method: :delete,
                                       data: { confirm: "You sure?" } %>
    <% end %>

_micropost.html.erb

<ol class="microposts" id="microposts_profile">
  <%= render @microposts %>
</ol>
<%= will_paginate @microposts %>

Do you have any idea to handle this problem?

Aucun commentaire:

Enregistrer un commentaire