Jorge Israel Peña Jorge Israel Peña - 3 years ago 155
Ajax Question

how to load an html fragment in rails with jquery?

I am wondering how I may

an HTML fragment returned by a model's
action. For example,
. The problem with this is that it returns the view I want embedded in the layout, as if I were to visit that url in the browser. I only want the
HTMl fragment.


Clarification: I want the
view to continue to render within the application layout when going to the
action in my browser. I'm merely trying to figure out how to fetch only the html of the
view for when I want to load it asynchronously with jQuery. I have seen guides in which they create
and then render the html view there, but it seems pretty messy to me, escaping all of that html for javascript. If this is the accepted and proper way of doing this though I guess I'll follow the standard. I was just wondering if it were possible to only fetch the html of a particular view.

Answer Source

You can use :layout => false in your render statement to return the fragment without the layout:

def show
  render(:layout => false) if request.xhr?
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download