在RAILS中用AJAX销毁微博如何重新加载当前页面
How to reload current page when destroy a micropost with AJAX in RAILS
伙计们!我开始学习了RAILS。
我有一个使用分页的微博列表。当我销毁微博时,它会转到第一页。但是我希望当我销毁微博时,它会重新加载当前页面。
这是我的代码:
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 %>
你有什么办法处理这个问题吗?
尝试将 page
参数添加到您的删除请求中,如下所示:
<% if current_user?(micropost.user) %>
<%= link_to "Delete", micropost_path(micropost, page: params[:page]),
remote: true,
method: :delete,
data: { confirm: "You sure?" } %>
<% end %>
I'm start to learn RAILS
欢迎光临!
简单修复:
#app/views/microposts/destroy.js.erb
location.reload(); // Reloads current page (the :page param should be predefined from the URL)
正确修复:
#app/views/microposts/index.html.erb
<%= render @microposts %>
<%= will_paginate @microposts %>
#app/views/microposts/_micropost.html.erb
<%= link_to "Delete", micropost_path(micropost, page: params[:page]), remote: true, method: :delete, data: {confirm: "You sure?"} if current_user? == micrpost.user %>
#app/controllers/microposts_controller.rb
class MicropostsController < ApplicationController
before_filter :authenticate
respond_to :js, :html
def index
@microposts = current_user.feed.paginate
end
def destroy
@micropost = Micropost.find params[:id]
@microposts = current_user.feed.paginate(page: params[:page]) if @micropost.destroy
end
private
def authenticate
return unless logged_in?
end
end
这将允许您更新以下内容:
#app/views/microposts/destroy.js.erb
$("#microposts").html("<%=j render @microposts %>");
伙计们!我开始学习了RAILS。 我有一个使用分页的微博列表。当我销毁微博时,它会转到第一页。但是我希望当我销毁微博时,它会重新加载当前页面。
这是我的代码:
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 %>
你有什么办法处理这个问题吗?
尝试将 page
参数添加到您的删除请求中,如下所示:
<% if current_user?(micropost.user) %>
<%= link_to "Delete", micropost_path(micropost, page: params[:page]),
remote: true,
method: :delete,
data: { confirm: "You sure?" } %>
<% end %>
I'm start to learn RAILS
欢迎光临!
简单修复:
#app/views/microposts/destroy.js.erb
location.reload(); // Reloads current page (the :page param should be predefined from the URL)
正确修复:
#app/views/microposts/index.html.erb
<%= render @microposts %>
<%= will_paginate @microposts %>
#app/views/microposts/_micropost.html.erb
<%= link_to "Delete", micropost_path(micropost, page: params[:page]), remote: true, method: :delete, data: {confirm: "You sure?"} if current_user? == micrpost.user %>
#app/controllers/microposts_controller.rb
class MicropostsController < ApplicationController
before_filter :authenticate
respond_to :js, :html
def index
@microposts = current_user.feed.paginate
end
def destroy
@micropost = Micropost.find params[:id]
@microposts = current_user.feed.paginate(page: params[:page]) if @micropost.destroy
end
private
def authenticate
return unless logged_in?
end
end
这将允许您更新以下内容:
#app/views/microposts/destroy.js.erb
$("#microposts").html("<%=j render @microposts %>");