gpt4 book ai didi

ruby-on-rails - rails 4 : Redirect to another action with js

转载 作者:行者123 更新时间:2023-12-04 05:57:50 26 4
gpt4 key购买 nike

在我的应用程序中,我使用 show.html.erb 页面显示操作,我在其中显示了该食谱的食谱和评论。我想添加 ajax 功能来添加评论。在 Controller RecipeController 中,我有 add_new_comment 操作。我可以从 add_new_action 重定向来显示来自 format.js 的 Action 吗?

显示 行动:

def show
@category = Category.where(id: @recipe.category_id).take
@comments = @recipe.comments.recent.all.paginate(page: params[:page], per_page:15)
end

add_new_comment 行动:
def add_new_comment
@recipe = Recipe.find(params[:recipe_id])
if params[:comment].present?
@comment = @recipe.comments.build
@comment.comment = params[:comment]
@comment.user = current_user
respond_to do |format|
if @comment.save
format.html {redirect_to recipe_path(@recipe)}
format.js {render action: 'show', :id => @recipe.id}
end
end
end
end

我有 add_new_comment.js.erb
$('#comments').html("<%= j (render 'comments') %>");

最佳答案

您可以使用 window.location.replace 或 window.location.href,如 here 所述

只需将该行添加到您的 add_new_comment.js.erb 中,它就可以工作了。

$('#comments').html("<%= j (render 'comments') %>");
window.location.replace("<%= recipe_path(@recipe) %>");

关于ruby-on-rails - rails 4 : Redirect to another action with js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29258949/

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