1

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).

3
  • I don't tell Rails 3.0.9 to render JS w/o a template; is it possible it's something in active_scaffold? Commented Sep 11, 2011 at 4:36
  • is there a layouts/admin.js? This is odd behavior. Rails shouldn't be rendering a JS template in an HTML layout. If there's a JS layout on the other hand... Commented Sep 11, 2011 at 4:49
  • @Dave Newton That's what I'm thinking. It must be something in active_scaffold. @numbers1311407 No, there is no layouts/admin.js. I inspected the content of the response and it's wrapping the RJS template in the HTML layout. Commented Sep 11, 2011 at 17:52

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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 

1 Comment

That's not correct; stock Rails won't layout a JS format with an HTML layout.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.