Question

I have a rails app with about 20 views. On 15 of those views I want to the same style to be reflected - Navbar on top, same buttons, etc. Right now, that's what I have.

However, on the other 5 views I want a specific style that is different than the above. Right now, when I create a new view, each view has the same style as the other 15. I'm assuming this is because my application.html.erb file contains that specific styling.

How can I create a custom layout for a specific view/set of views that is totally different than the majority? It looks like the custom styles are being referenced from app/views/layouts, but if that's the case what naming convention do I use to create a new layout and how do I refer to that layout from my view?

Was it helpful?

Solution

One way you can do this is in the controller. You can create a new layout called custom_layout.erb in app/views/layouts and then in your controller, you can reference the layout in the action

def show
 render layout: "custom_layout"
end

Or for the whole controller

class HomeController < ApplicationController
  layout "custom_layout"

  def show
  end
end

Check out the Rails Guide - Layouts and Rendering Sections 2.2.11.2 and 2.2.12 for more examples

Hope this helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top