Create a new URL path for each user in RoR

2020-07-23 09:05发布

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.

4条回答
等我变得足够好
2楼-- · 2020-07-23 09:42

Have a look at subdomain-fu plugin.

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

查看更多
ら.Afraid
3楼-- · 2020-07-23 09:43

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 :)

查看更多
我想做一个坏孩纸
4楼-- · 2020-07-23 09:44

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.

查看更多
Viruses.
5楼-- · 2020-07-23 09:48

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.

查看更多
登录 后发表回答