Custom views and assets for tenants in a multi-tenant rails app?
Asked Answered
D

1

7

When using a rails engine, overriding views is as easy as creating new views in the right folder. But when creating a multi-tenant rails app where all tenants reside in the same app (they don't use an engine), how can one override views for tenants.

For example:

App has a views/static/about_us.haml file which needs to be customized for each tenant. What's the best way to override this file for each tenant?

Deprecative answered 8/6, 2015 at 7:7 Comment(1)
Looking at the name, it sounds like a page that can be completely customized. I'd save tenants' custom page in some rich form after applying some sanity checks to prevent XSS etc; and display it without HTML escaping. Otherwise, e.g. if there are a few well defined tenants, different files/folders for each: views/tenant1/static/about_us.haml, views/tenant2/static/about_us.haml.Oleaster
R
1

I use the apartment gem which is nice for managing multi tenant environments. Apartment helps you managing schema-based database and helps you with the migration stuff.

The app uses the right schema depending on the subdomain. For example, in the case of superclient.mysuperapp.com, rails will use the superclient database schema and will work only on this schema until the request finishes.

For multitenant views, in my case I use a before_action in ApplicationController.rb to prepend my custom view path:

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

Where subdomain in this case is superclient.

The logic is this: first, rails will look for a view in this path: "app/views/multitenancy/#{subdomain}". And if it doesn't find anything, it continues to find the view in the other paths on the list.

I hope my response will help you.

Rationale answered 23/2, 2018 at 3:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.