使用rails 3.1中的ajax调用渲染部分视图

时间:2012-05-21 12:40:48

标签: ruby-on-rails ajax ruby-on-rails-3.1 erb renderpartial

我现在开始使用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实例?

谢谢, 若昂

1 个答案:

答案 0 :(得分:8)

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