Create a new URL path for each user in RoR

2020-07-23 09:16发布

问题:

How can I create in real-time a new URL path in Ruby on Rails?

For example: I want my users to have name.XXX.com or XXX.com/name.

FYi I'm hosting the code on Heroku.

回答1:

Have a look at subdomain-fu plugin.

https://github.com/mbleigh/subdomain-fu



回答2:

I would checkout some previous answers for 'vanity' urls in the case for example.com/username:

How can I implement vanity URL's in a Rails application?

How to implement "short" nested vanity urls in rails?

As for subdomains, other suggestions by @Arkan, @Anton, @tadman are a good starting point.



回答3:

You'll need a wildcard CNAME or A record to point your names over in the first place, which in this case would be *.XXX.com, to handle this on a DNS level. The second part is in examining your request.host header which will be name.XXX.com or whatever is used. You can use this to set variables in a before_filter block.



回答4:

The easiest solution is like tadman described: set up a DNS record and write a rewrite rule for Apache/nginx/whatever which will eat subdomain and add it to params.

As an option, set up a middleware (more about it http://guides.rubyonrails.org/rails_on_rack.html), where you can rewrite/route the requests before the get processed by Rails routing. Being prepared carefully, this can help implementing very sophisticated things :)