How to load html fragment in rails using jquery?

I am wondering how can I get the jQuery.loadHTML fragment returned by the model action show. Eg $("#container").load("/posts/34"). The problem is that it returns a view that I want to embed in the layout, as if I had to visit this URL in a browser. I need only an Post showHTMl fragment .

Thank!

Explanation . I want the view showto continue to render in the layout of the application as it proceeds to action showin my browser. I'm just trying to figure out how to get only html views showwhen I want to load it asynchronously using jQuery. I saw the manuals in which they create show.js.erband then render the html view there, but to me it seems pretty messy, avoiding all this html for javascript. If this is an acceptable and correct way to do this, I think I will follow the standard. I'm just wondering if it is only possible to get html of a certain kind.

+5
source share
3 answers

: layout = > false :

def show
  ...
  render(:layout => false) if request.xhr?
end
+8

GET URL- :

def show
  render(:layout => false) if params['partial'] == 'true'
end

javascript:

$('#container').load('/posts/34?partial=true')
+2

If you want to support regular and AJAX requests, you want to use a block respond_to.

In this way

respond_to do |format|
  format.html # renders index.html.erb automatically
  format.js { render 'show', :content_type=>'text/html', :layout=>false }
end
+2
source

All Articles