views:

58

answers:

2

My controller is shared by links from a search result page that needs a layout, and from the profile page itself that does not 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?

A: 

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
Yannis
The reason that wouldn't work. Is that I'm trying to **not** use the `script` call in the ajax. So the format.html would have to be set to `false`. But that's being used by both queries. Ha, sorry if this is confusing. I've been re-editing it to make it as simple as possible :)
Trip
It's also worth noting that as unexplicable as it is, that when you set an AJAX request to script, it skips the layout param.
Trip
A: 

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") %>');
Stevens
Like I say above I **can not** use the `script` because when I pass script through AJAX calls on user-generated content it creates on 1 out of a 100 calls an unterminated string error. So it **has** to be HTML in the call. Otherwise, no problem. I had your answer originally :D
Trip