I have three layouts in my Rails app. I’m finding that they all work exactly as expected on my development machine, but when I deploy to production, views render with the wrong layouts.
For example, I have this…
class AgendasController < ApplicationController
layout('main')
def show
@agenda = Agenda.find(params[:id])
render( :action => :show, :layout => 'agenda')
end
# etc ...
end
I want all views to render with the “main” layout except have show render with the “agenda” layout.
As I said, it’s working perfectly in development, but in production the show view renders with the “main” layout instead of the “agenda” layout as expected.
I’ve also tried adding…
layout('main', :except => :show)
But that doesn’t help — same result.
I’m using Rails 2.3.11 with Passenger
My gem environment in production in case it helps…
RubyGems Environment:
- RUBYGEMS VERSION: 1.3.5
- RUBY VERSION: 1.8.7 (2009-12-24 patchlevel 248) [i686-linux]
- INSTALLATION DIRECTORY: /opt/ruby-enterprise-1.8.7-2010.01/lib/ruby/gems/1.8
- RUBY EXECUTABLE: /opt/ruby-enterprise-1.8.7-2010.01/bin/ruby
- EXECUTABLE DIRECTORY: /opt/ruby-enterprise-1.8.7-2010.01/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86-linux
- GEM PATHS:
- /opt/ruby-enterprise-1.8.7-2010.01/lib/ruby/gems/1.8
- /home/emicha/.gem/ruby/1.8
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- http://gems.rubyforge.org/
I’ve never seen this problem before and all the other apps on the server are working fine.
UPDATE
Production log claims that it’s using the “agenda” layout, but what’s showing up in the browser is the “main” layout. I can tell because of the appearance and also from looking at the HTML source — there are JS files included that are not in the “agenda” layout.
Processing AgendasController#show (for 10.1.1.136 at 2011-09-02 13:22:00) [GET]
Parameters: {"action"=>"show", "id"=>"1682", "controller"=>"agendas"}
Rendering template within layouts/agenda
Rendering agendas/show
Solution
OK here’s what was going on. In “main” layout I had something like…
And in “agenda” layout I had just…
In the development environment, Rails doesn’t cache these assets (i.e., concatenate them together), so everything worked fine. But in production it does when you have
:cache => true.And it was also including the concatenated JS file with the agenda layout (even though that layout doesn’t use any JS and it wasn’t linked in the head element).
The unexpected styling and presence of the JS “.all” file lead me to believe it was using the wrong layout for the agenda show view.
It turns out that Rails was using the right layouts all along, but apparently including all concatenated asset files everywhere.
The solution was to change the “agenda” layout to have this…