Pergunta

I need to do this for a controller which uses the active_scaffold gem. We have a controller that looked something like this:

class Admin::UsersController < ApplicationController
   layout 'admin'

   active_scaffold :users do |config|
     config.search.columns = [:first_name, :last_name]
   end
end

That worked great when we were on Rails 2.3.10, but we're upgrading to Rails 3.0.10. As part of the upgrade, I had to upgrade active_scaffold (currently installed from the rails-3.0 branch of git://github.com/activescaffold/active_scaffold) to be compatible. One problem we were having is that searching the table wasn't working. I would see in my log files:

Rendered <snip>/gems/active_scaffold-25b3d724f35b/frontends/default/views/list.js.rjs within layouts/admin (923.5ms)

Notice that it's rendering the RJS template with the layout specified in the controller. That seems like an unreasonable default to me. Shouldn't RJS templates render without a layout by default? Anyway, I fixed it as such:

class Admin::UsersController < ApplicationController
  layout :admin_layout

  private

  def admin_layout
    respond_to do |format|
      format.js   { false }
      format.html { 'admin' }
    end
  end
end

That fixes the issues with search and pagination. (The RJS template is now rendered without a layout, so the browser can execute the resulting Javascript). I guess my question is, why do I have to tell Rails that it shouldn't render RJS templates with layouts? And is there a better solution? This feels like too much of a hack to me (the bad kind of hack---the kind of hack that will break in the future).

Foi útil?

Solução

Okay, I figured it out. @numbers1311407's comment under my question led me to check the name of the layout template. It was layouts/admin.haml. With Rails 2, that layout was only rendering for HTML requests, but with Rails 3 it applies to all requests (because it doesn't specify a format type). I renamed it to layouts/admin.html.haml and it works with a simple layout 'admin' in my controller (as opposed to the hack that I had come up with in my question).

So the answer to the question, "Why does Rails render RJS templates within a layout?" is that the layout file was named such that it applies to all formats.

Outras dicas

Answer your quastions:
1. There is no magic that Rails renderers layout in for JS format. That's bacause it is default to Rails to render layout with any template unless you explicitly tell to avoid it. You can just look into Rails sources in file: actionpack/lib/action_controller/metal/renderers.rb to see :js renderer.

2.Try to use:

respond_to do |format|
  format.js { render *your_any_options*, layout: false }
end
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top