Topic: Subdomains based upon a tag...

I'm having issues implementing a subdomain system for my rails app...

I have looked at Request Routing Plugin and account_location but they don't seem to be what I want.

My final aim is to run one application that three urls feed off.  A business site, a work blog and a skiing blog (work and business are two different blogs - different businesses but related...).

I have a Posts controller and from it I want to be able to write a post and depending on what it is tagged with - this will determine on which site it shows up on.

Currently I have working (in apache2) - a.localhost:3000 (work blog), b.localhost:3000 (ski blog) and localhost:3000 (business blog and site) - but obviously there is no content on these sites - this is where I'm stuck.

What I think I'm to do is to create a before_filter in my applicaiton.rb that checks what domain I'm on first (a.localhost:3000) and only show the posts that are associated with the tag that is associated with a.localhost:3000 (one tag per domain - I may change this to a category but for now it is fine).

If anyone has any ideas I would love to hear them - if this is unclear just let me know and I'll try and rephrase.


Last edited by Coopr (2008-01-19 01:40:06)

Re: Subdomains based upon a tag...

So for you need to display posts tagged with "a"?
Sounds like you can check your request.env and find what you need. 

class SomeController < ...
  def blog
     @posts = Post.find_tagged_with(request.env[<one of the params>].gsub("",""))

Just for example, for me that would be request.env["HTTP_X_FORWARDED_HOST"].
For example - or or http://<whatever><anything>, check those to see what my request.env["HTTP_X_FORWARDED_HOST"] would look like.
You may be able to do the same.

Re: Subdomains based upon a tag...

Take a look at this wiki page.  Instead of using the subdomain as an account key, you would use it as a tag key. does something similar to filter the feeds (IE: Posts with hiring in them).

Re: Subdomains based upon a tag...

Ok thanks - I kind of get what you guys are talking about.

I'm trying to implement each idea and see which one works / works better.

Firstly PullMonkey:

class PostsController < ApplicationController
    @posts = Post.find_tagged_with("skilife".gsub("localhost:3000", ""))                           
    respond_to do |format|

This works.  But replacing "skilife"
doesn't.  I get this error:

You have a nil object when you didn't expect it!
The error occurred while evaluating nil.gsub

When I run <%= request.env %> in the view (just to see what it's outputting) I get a lot of blah blah:

SERVER_NAMEworklife.localhostPATH_INFO/postsHTTP_ACCEPT_ENCODINGgzip,deflateHTTP_USER_AGENTMozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv: Gecko/20071127 Firefox/,en;q=0.5HTTP_HOSTworklife.localhost:3000REMOTE_ADDR127.0.0.1SERVER_SOFTWAREMongrel 1.1.3HTTP_KEEP_ALIVE300REQUEST_PATH/postsHTTP_COOKIE_coopdd_session=BAh7BiIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNo%250ASGFzaHsABjoKQHVzZWR7AA%253D%253D--bad9cab8075afd72afa75db0dea25145b3613195HTTP_ACCEPT_CHARSETISO-8859-1,utf-8;q=0.7,*;q=0.7HTTP_VERSIONHTTP/1.1REQUEST_URI/postsSERVER_PORT3000GATEWAY_INTERFACECGI/1.2HTTP_ACCEPTtext/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5HTTP_CONNECTIONkeep-aliveREQUEST_METHODGET

Which I assume is just telling me about my environment...

Any ideas?


I haven't had a chance to implement your idea yet - I have read through the wiki page though and it looks like it's adding a layer of complexity I may not need - but it is my next step.

Thanks for your help smile

Re: Subdomains based upon a tag...

Just had a quick thought - I looked through the request.env and saw that SERVER_NAME was outputting the subdomain that I wanted. So...

class PostsController ...
  def index
    @posts = Post.find_tagged_with(request.env["SERVER_NAME"].gsub(".localhost", ""))

This gets the result that I want.

Now my other question - nothing stops someone from putting rails.localhost in there and that would return no results....

I think I should do a catch all route here?  What do you think?