Change Rails’ layout via controller

I’ve seen a few people asking me how to specify a specific layout in their controllers. Simple:

#my admin/condos_controller.rb
class Admin::CondosController < ApplicationController
  layout 'layouts/admin'
end

Tags:

15 comments

  1. I can’t figure out how to get a specific controller method to use a different layout. Your example works at the top of the controller, but how do I get “show” to use a different layout than “new” does? “render” doesn’t seem to work anymore.

  2. I can’t figure out how to get a specific controller method to use a different layout. Your example works at the top of the controller, but how do I get “show” to use a different layout than “new” does? “render” doesn’t seem to work anymore.

  3. Steve, try something like this inside your method:

    render :action => "index", :layout => "help"
    

  4. Steve, try something like this inside your method:

    render :action => "index", :layout => "help"
    

  5. Jonathan – thanks! I appreciate your help and interest. I’m sorry, I don’t follow which “method” you are suggesting I place that directive in. Assuming we are talking about the controller, I’ve got the basic 2,0 RESTful controller with at present the basic seven methods. I want all except “show” to use a common layout, but I want “show” to use a different one. When I put anything that begins with “render” inside the show method I get an error informing me that too many redirects have been requested.

  6. Jonathan – thanks! I appreciate your help and interest. I’m sorry, I don’t follow which “method” you are suggesting I place that directive in. Assuming we are talking about the controller, I’ve got the basic 2,0 RESTful controller with at present the basic seven methods. I want all except “show” to use a common layout, but I want “show” to use a different one. When I put anything that begins with “render” inside the show method I get an error informing me that too many redirects have been requested.

  7. Steve, this should make sense, if not, let me know.

    class DashboardController  "secondary"
      end
    
    end
    
  8. Steve, this should make sense, if not, let me know.

    class DashboardController < ApplicationController
      layout 'primary'
    
      def index
        @everything = Stuff.find(:all)
      end
    
      def show
        @something = Stuff.find(params[:id])
        render :layout => "secondary"
      end
    
    end
    
  9. Sweet – thanks!
    I had tried what you have in line #10 above, but it had *not* occurred to me to put what you have in line #2. Did both as you suggest and the app doesn’t complain now. Thanks so much for stocking with this for me!

  10. Sweet – thanks!
    I had tried what you have in line #10 above, but it had *not* occurred to me to put what you have in line #2. Did both as you suggest and the app doesn’t complain now. Thanks so much for stocking with this for me!

  11. I had the same problem trying to figure that out :) Glad I could help!

  12. I had the same problem trying to figure that out :) Glad I could help!

  13. Will that work for RJS templates? i.e. format.js?

  14. Will that work for RJS templates? i.e. format.js?

Leave a comment