开发者

Create a new URL path for each user in RoR

开发者 https://www.devze.com 2023-01-25 22:09 出处:网络
How can I creat开发者_StackOverflow社区e 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.

How can I creat开发者_StackOverflow社区e 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.


Have a look at subdomain-fu plugin.

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


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号