3

我现在从 Rails 开始,我认为我有一个简单的问题。我需要在一个 ajax 调用中渲染两个部分:

我有以下控制器:

# 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 我该怎么做?默认情况下,render @host 方法将使用模板文件“_host.html.erb”。我怎样才能调用另一个像 _host2.html.erb 并拥有相同的 @host 实例可用?

谢谢,若奥

4

1 回答 1

9
$('#tabs-1').html("<%= j(render @host) %>");
$('#tabs-2').html("<%= j(render :partial => 'host2', :locals => { :host => @host }) %>");
于 2012-05-21T13:02:57.750 回答