Save 36% for Black Friday! Learn more

All threads / Change Layout based on current URL

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Change Layout based on current URL

Sascha M. asked in Rails

Hi all,

let's say I wand to build a whitelabel product.

I want to display different layouts (Logos, CSS, ...) based on the current URL the website was accessed by.

E.G.
a123.mywebsite.com should render a different layout than
b124.mywebsite.com

I couldn't find anything describing something like that anywhere :(

Thanks in advance!

As a really trivial example, you can do what they mentioned on StackOverflow here: https://stackoverflow.com/questions/6748168/changing-rails-3-apps-layout-based-on-the-hostname-domain-for-branding

  layout :domain_layout

    def domain_layout
    controller.request.host
    end

Which would look for app/views/layouts/a123.mywebsite.com.html.erb, etc.

You can do other tweaks to that to decide which file to render.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 39,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.