gpt4 book ai didi

ruby-on-rails - 在 rails 3.1 中使用 ajax 调用渲染局部 View

转载 作者:行者123 更新时间:2023-12-04 03:46:07 25 4
gpt4 key购买 nike

我现在从 Rails 开始,我只是想提出问题。
我需要在一个 ajax 调用中渲染两个部分:

我有以下 Controller :

# GET /hosts/1
# GET /hosts/1.json
def show
@host = Host.find(params[:id])

respond_to do |format|
format.html #show.html
format.js
format.json { render :json => @host }
end
end

以及相应的模板(show.js.erb):
$('#tabs-1').html("<%= escape_javascript(render @host) %>");

和一个名为 _host.html.erb 的部分文件

所有这些工作正常。模板“_host.html.erb”在 div tabs-1 中呈现,但现在我需要在不同的 id (#tabs-2) 中添加一些其他部分模板,但使用相同的 @host
我怎样才能做到这一点?默认情况下,渲染@host 方法将使用模板文件“_host.html.erb”。我怎样才能调用一个不同的,比如 _host2.html.erb 并且有相同的 @host 实例可用?

谢谢,
若昂

最佳答案

$('#tabs-1').html("<%= j(render @host) %>");
$('#tabs-2').html("<%= j(render :partial => 'host2', :locals => { :host => @host }) %>");

关于ruby-on-rails - 在 rails 3.1 中使用 ajax 调用渲染局部 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10685462/

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