开发者

Is there an AJAX function that can just GET a rails partial?

My controller is shared by links from a search result page that needs a layout, and from the profile page itself that does no开发者_JAVA技巧t need a layout. What I would like to accomplish is a single controller method show that is both capable of drawing the partial by AJAX from the profile page, and draw the partial and layout from the search results.

The most important restriction I have is that I can not set the dataType: to script . It has to be html . So I can't use that spiffy AJAX script call that renders the JS without the controller's format.html getting involved.

tabs_controller.js

def show
  @organization = Organization.find(params[:organization_id])
  @tab = @organization.tabs.find(params[:id])
  respond_to do |format|
    format.html { render :partial => 'tab', :layout => 'myHQpage' }
    format.js
  end
end

javascript

$.get($(this).attr('href'), null, null, "html");

show.html.haml

= render :partial => 'tab'

What AJAX function can I use here that just draws the partial, and not the entire layout?


I'm not sure to really understand but this may help you. Yo can add a :layout => false to the format.js block.

def show
  @organization = Organization.find(params[:organization_id])
  @tab = @organization.tabs.find(params[:id])
  respond_to do |format|
    format.html { render :partial => 'tab', :layout => 'myHQpage' }
    format.js { render :partial => 'tab', :layout => false }
  end
end


Keep the 'script' dataType setting and just add a wrappertag around your search results. Then use the jQuery replaceWith function to replace the content of that tag:

_tag.html.erb:
<div id="search">
  ...
</div>

show.js.erb:
$('#search').replaceWith('<%= escape_javascript(render "tab") %>');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜