Question

I Have a Rails app that contains a large number of resources.

The app is split into two pieces: a public facing area exists in the naked domain:

example.com

And an admin version inhabits the admin subdomain:

admin.example.com

Each of the resource controllers have the standard CRUD actions available to admin users who are signed in and are in the admin subdomain, including admin-specific views for index and show actions.

The public facing area also has index and show actions for each of the resources, but their views are very different form the admin view.

So I need to display different index and show views for each resource depending on subdomain.

How can I do this whilst avoiding littering my controllers with logic operators checking the current subdomain? It doesn't make sense to split each controllers into separate admin and public flavours because all that is different is the views that are rendered for index and show.

Was it helpful?

Solution

One option is to create a view folder for each sub-domain app/views/admin and put all the view files for that sub-domain in it e.g. app/views/admin/foos/index.html.erb.

Then use the prepend_view_path to change the path based on the sub-domain like this:

 class ApplicationController < ActionController::Base
   before_filter :prepend_view_paths

   def prepend_view_paths
     subdomain = request.subdomain 
     prepend_view_path "app/views/#{subdomain}"
   end
 end

The prepend method adds the given path to front of the queue and if it is not found, it searches for the file in the default paths.

Warning: Code not tested

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