gpt4 book ai didi

ruby-on-rails - RAILS中使用AJAX销毁微博时如何重新加载当前页面

转载 作者:数据小太阳 更新时间:2023-10-29 08:44:40 25 4
gpt4 key购买 nike

伙计们!我开始学习 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

销毁.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 %>

你有什么想法来处理这个问题吗?

最佳答案

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 %>");

关于ruby-on-rails - RAILS中使用AJAX销毁微博时如何重新加载当前页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33298498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com