Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering partial views with ajax calls in rails 3.1

I'm starting now with rails, and i have simply question i think. I need to render two partials in one ajax call:

I have the following 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

And the respective template (show.js.erb):

$('#tabs-1').html("<%= escape_javascript(render @host) %>");

And a partial file named _host.html.erb

All this works fine. The template "_host.html.erb" is rendered in div tabs-1, but now i need to add some other partial template in a different id (#tabs-2), but use the same @host How can i do this? by default the render @host method will use the template file "_host.html.erb". How can i call a different one like _host2.html.erb and have the same @host instance available?

Thanks, Joao

like image 202
Orange Avatar asked Dec 01 '25 05:12

Orange


1 Answers

$('#tabs-1').html("<%= j(render @host) %>");
$('#tabs-2').html("<%= j(render :partial => 'host2', :locals => { :host => @host }) %>");
like image 198
cutalion Avatar answered Dec 03 '25 21:12

cutalion



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!