0

I have a single Rails application that is currently being accessed via lets say https://www.domain1.com and I would like to enable the application to be accessed by a second domain, lets say https://www.domain2.com which would then set a global instance variable.

I would then like to use this instance variable to control certain things on the page such as content, styles and images.

Any ideas how I could achieve this?

1 Answer 1

2

E.g.

def my_action
  @domain_instance_variable = request.domain
  $domain_global_variable = request.domain
end

A global variable and an instance variable are different things. An instance variable is scoped to an instance of a class, and a global variable has a global scope. It's a good rule of thumb to minimize scope.

In the case of Rails code in controllers, views or helpers, you can just call request.domain directly instead of assigning that to a variable.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this worked nicely. I ended up adding a before_action into my application controller that would set the instance variable and then I pick that up in my views.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.