Render partial with Ajax!

So I had some issues with an App I’m doing, I was calling an entire template…

The problem was that when you call a template, you load all the libraries with it… its just crap.

So I create a partial to just call the partial for every search, without the libraries or anything that I don’t need.

To do that you must specify the partial in the respond_to like this



respond_to do |format|

      format.html { render :partial => 'partials/results' } # index.html.erb

      format.json { render json: @results, :callback => params[:callback] }

end


And with jquery you just call the data



$.get "/search/result", data, (data) ->

      $("#resultados").html data


Maybe some of you already know that, but I’m just letting this here so maybe it can be helpful to someone else.

Thanks for sharing your discovery, @robzdc;
I hope to see more posts with valuable information we can share with fellow Rubyists.

OR what would be even better than that, keep your results partial, but have a results.html.erb file AND a results.js.erb file. Render the partial per usual in the results.html.erb file. In the js.erb file I usually put my content in a variable

var content = $('<%= escape_javascript(render "PARTIAL_NAME") %>');

and then you can append to page or do whatever you want with the data. That way you still have a standard response to a browser with js disabled.
Don’t forget to add format.js to your respond_to block. And the get action, just append .js to the url.

I tried that, but it was appended like text.
Im using CoffeeScript