Search code examples
ruby-on-railsdashboard

Differentiating between a dashboard layout and other view


There is probably no right answer for this, but I would like to get input on what the best practices are.

I have a rails application built already. In the app, there is a dashboard view and a regular view. In the application.html.erb I will include the header and the footer for both views, but what is the best practice to differentiate between the 2 views?

The two ways I've been thinking about are:

  1. Include a variable (is_dashboard_view) and check it.
  2. Change the URL's to have a common pattern, like /dashboard/...

I am leaning towards option 1 since it seems to be a less of a hassle. Are there any other options and what would be the best practice.


Solution

  • Here you need to create a new layout dashboard.html.erb similar to application.html.erb. In this new layout you can use only those assets that you want to load for dashboard view and You can use a specific layout like this ....

    class MyController < ApplicationController
      layout 'dashboard'
    end
    

    For rails layout per action you can use a method to set the layout.

    class MyController < ApplicationController
      layout :resolve_layout
    
      # ...
    
      private
    
      def resolve_layout
        case action_name
        when "new", "create"
          "some_layout"
        when "index"
          "other_layout"
        else
          "application"
        end
      end
    end