Ask A Question

Notifications

You’re not receiving notifications from this thread.

Domain & Subdomain Routing Constraints in Rails Discussion

@Chris !!!!

THANK YOU! I've been at this all day working with acts_as_tenant. Had some issue with some "undefined method `arity' " error when I added the acts_as_tenant(:account) to a model. Nothing much on the internet - but turned out it was such a simple updating of the gem to resolve it. (just in case anyone else comes across this issue like me). It's 2am here now. Tired. EEEK!

Anyway, thank you so much for explaining the final piece of the puzzle for multi-tenancy domain constraints in detail.. Appreciate it. Cannot wait to try out this with a custom domain on a live site. Going to try host the app using Hatchbox. I normally use Heroku but going to make the switch now.

Savage work dude!!
Bradley.

Reply

This episode is right on time, just what I needed. Quick little detail, around 1:50, I had to do more than set config.hosts =nil. I also had to comment out config.hosts in environments/development.rb before the app would start.

Reply

FYI @chris, you actually can use localhost (well at least on Firefox and Chrome).

localhost will act as the top-level domain, so if in dev you use an URL like: my-subdomain.my-app.localhost:3000, rack will consider the request.subdomain to be my-subdomain. So no need for lvh.me anymore ;)

Reply

that was my thoughts too. And on any unix type of operating system that you manage you can also add a few more lines in the hosts file to make it work with any browser as this file is queried before the DNS.

Reply

I recently created a DomainConstraint in an app and used a database query which checks to see if there is a match with any of the saved domains. I do see now that it does add the query to every request but the cost is just a couple of milliseconds. I suppose that I could figure out a way to avoid the database query by caching the saved domain values or instead setting a general constant for the main (i.e. non-custom) domains and doing a non-match against that. I wonder though if it is worth the effort at this point not to mention the added complexity. Are there other hidden costs beyond the few milliseconds I should be considering?

Reply

thank you very helpful, I'm using render.com any idea how to have the user put in a domain in my app and have it automatically add to render? so they can then use the domain

Reply

This has helped me a lot! Thanks, folks!

One thing I'm stuck on is how to write the specs when I have a domain constraint. I've been trying to figure out how to force my request to go through a specific domain, instead of localhost. Is that possible at all?

Reply

You can use host! "subdomain.example.org" to change the host for a test. 👍

Reply

Oh, I see. Thanks, Chris!

I had to include RSpec::Rails::RequestExampleGroup so it'd allow me to use host!. I'm trying to figure that out now - good thing the specs are working now, though :)

Reply

Thanks, I'm happy to see u here

Reply
Join the discussion
Create an account Log in

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

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

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

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2024 GoRails, LLC. All rights reserved.