Learn how to unleash the full potential of subdomains with the subdomain-fu Rails plugin in this episode.
Domain Name Solutions
As mentioned in the episode, you’ll need to add each subdomain to /etc/hosts. This can be a problem if subdomains can be generated dynamically. It would be nice if we could add a catch-all domain. Here are a couple solutions.
One is to make a PAC file and set it up as the proxy in your web browser preferences. It might look something like this.
Another solution is to set up your own domain name server on your localhost and create an A entry to catch all subdomains. Sorry I can’t provide detailed instructions for this. Use Google. :)
Source Code from Episode
Domain Name Solutions
As mentioned in the episode, you’ll need to add each subdomain to /etc/hosts. This can be a problem if subdomains can be generated dynamically. It would be nice if we could add a catch-all domain. Here are a couple solutions.
One is to make a PAC file and set it up as the proxy in your web browser preferences. It might look something like this.
# proxy.pac
function FindProxyForURL(url, host) {
if (shExpMatch(url,"*.local/*")) {
return "PROXY localhost";
}
return "DIRECT";
}
Another solution is to set up your own domain name server on your localhost and create an A entry to catch all subdomains. Sorry I can’t provide detailed instructions for this. Use Google. :)
Source Code from Episode
script/plugin install git://github.com/mbleigh/subdomain-fu.git
sudo apachectl graceful
dscacheutil -flushcache
# /etc/hosts
127.0.0.1 personal.blog.local company.blog.local
# apache config
ServerAlias *.blog.local# routes.rb
map.blog_root '', :controller => 'blogs', :action => 'show', :conditions => { :subdomain => /.+/ }
map.root :blogs
# initializers/subdomain_config.rb
SubdomainFu.tld_sizes = {:development => 1,
:test => 0,
:production => 1}
# controllers/application.rb
def load_blog
@current_blog = Blog.find_by_subdomain(current_subdomain)
if @current_blog.nil?
flash[:error] = "Blog invalid"
redirect_to root_url
end
end
# articles_controller.rb
before_filter :load_blog
def index
@articles = @current_blog.articles.find(:all)
end
def show
@article = @current_blog.articles.find(params[:id])
@comment = Comment.new(:article => @article)
end
def new
@article = @current_blog.articles.build
end
<!-- blogs/index.html.erb -->
<%= link_to h(blog.name), blog_root_url(:subdomain => blog.subdomain) %>