I've been trying to get Devise and subdomains to work together, by using one of the Subdomain plugins, but not managed to get there fully. Going to have a go using your approach now and see how I get on.

Hi, Ryan.
I've wrote similar article some months ago (http://bit.ly/ajGym2 - sorry, only Russian version, but you can use Google Translate) but there are some problems with session sharing between subdomains in Opera browser - unfortunately Opera cannot read cookies from sibling domain - it means if you set cookie for dom1.example.com cookie cannot be read from dom2.example.com

There's a blog article on my.opera.com that describes how the Opera browser deals with cookie sharing between subdomains.

To summarize (very quickly) the article: any *.example.tld subdomains are considered as siblings (and can share domain=.example.tld cookies) if the example.tld domain has a registered IP address in DNS.

I just figured out how to get this working and blogged about it a couple of days before you did this! I like your solution more though. In case anyone is interested, I also blogged about how to test it with cucumber/capybara: http://mattbeedle.com/2010/07/04/testing-subdomains-with-cucumber-cabybara-and-rack-test

:domain => :all doesn't work in the Rails 3 release candidate (it results in an InvalidAuthenticityToken error). The issue is resolved in Rails 3 master on GitHub with commit fd78bb72704554737117 by Bryce Thornton on 14 Aug 2010.

Thanks, Ryan! For anyone who needs more, I put together a Rails 3 example app (with a detailed walk-through tutorial) that shows how to set up subdomains in Rails 3 using Devise for authentication: http://github.com/fortuity/rails3-subdomain-devise (click above for the link). For anyone who needs a quick start for blog-style subdomains using Rails 3.

which allows
personal.lv.me:3000/articles to correctly go to articles#index where I do a blog.find_by_subdomain as a before_filter.

However, the problem comes in a named route when I try generate a url. If I do

<%= link_to "View all articles", blogs_articles_path(@blog) %>

I will get lv.me:3000/blogs/1/articles which is correct.
However, if, as a user, I am browsing personal.lv.me:3000/ rails will generate personal.lv.me:3000/blogs/1/articles, which is incorrect as it should be personal.lv.me:3000/articles
I can rectify this by doing <%= link_to "View all articles", articles_path %>, and the path will be correct. But now it will be broken again if I browse without a subdomain.

Is there anyway to get rails to automatically remove the nested resource from the path if a subdomain is detected?

I'm not sure if you are still looking for the answer, but as I just went through this episode and I am going to use a similar functionality with subdomains, as you need, I guess I know the answer for your question.

First of all, you can not expect to have in return of _path helper methods anything with the host. Instead use the _url helper methods. That will lead you into the 2nd point.

Based on the root_url(:subdomain => @blog.name) used in the episode and thanks to the overridden url_for helper you can use the subdomain key in the options of any _url helper methods. Here we go:

I think you could also override blogs_articles_url helper method to be consistent with using nested resources way of creating urls. The below code is just an idea and should be re-factored to use all functionality of blogs_articles_url helper method, but for simplicity I put the below.

There's one important thing to know if you want to share sessions between subdomains on localhost. Using :domain => ".lvh.me" not always works (in my case it didn't), so you have to configure your environment as described in http://blog.plataformatec.com.br/2009/12/subdomains-and-sessions-to-the-rescue/

I wish I could delete the post right above. It was a bad typing problem.

But I have another problem, a real one this time: flash messages are not working properly. I'm using Devise and everything is ticking right about the user, but I'm experiencing some funny behavior. When I create a new resource that is accessible through a subdomain, at the main domain, and redirect right after to this new resource, the flash message persists until I go to the main domain.

Beyond that, other flash messages, like editing this resource, don't appear at all.

Any ideas? I'm using Rails 3.0.7 and did everything that our best buddy Ryan did.

Does anyone have a problem with Internet Explorer? I couldn't get the session store accross subdomains working well...

It works fine most of the times, but sometimes it seems the session is different between subdomains... example: if I log out from subdomain1.domain.com and after that I visit subdomain2.domain.com for some reason I am still log in!

The behavior is strange, but it just happen in Internet Explorer =/ and, as I said before, it only occure sometimes...

Hello, I am trying to implement the subdomains on my app. I am using rails 3.1.rc5. The problem is that "constraints" in my route.rb does not work and i am redirected to my app root. However if I use "constraint" (singular), my subdomain is parsed and i am succesfully routed to the show action of the controller i wanted (SchoolsController). The problem with this is that when i do not use a subdomain and therefore wish to be redirected to the root of my app, i get an "ActiveRecord::RecordNotFound" because it cannot find a School with that subdomain.

Hi, I am new on Rails and I really like this screen cast to implement on my first app. But wonder if this screen cast is applicable on a real domain, cause most of comments here are pointing to localhost.

I wan't to use it on my testing domain which is http://ngtv2.info/ and if possible, to have i.e. kingpangilinan.ngtv2.info

I seek advice about configuring the domain length, different for staging and production. I have an app that uses subdomain. The same app is hosted in a staging environment and production environment on the same server.

I am just running localhost via rails server, and somehow I can't get any of the subdomains to load any pages. So lets just say if I use user1.example.com:3000 (i run my development on a remote server), the page just doesn't load.

And example.com:3000 works. I've read multiple tutorials, but wasn't able to find an answer to this.

I've also tried this on my windows PC, and have set these in host file 127.0.0.1 subdomain1.localhost.local company.blog.local personal.blog.local, but this doesn't work either.

What got me in this was editing my own /etc/host file to have '127.0.0.1' point to my own custom wildcard domain '*.local.me'

After some research, I discovered you can't have a wild card in your /etc/host file. When Ryan says to use 'lvh.me:3000', literally use that rather than editing your /etc/host file. You can then use your subdomains.

Hey,
When I first try lvh.me:3000, it is working fine. But now I have problem with lvh.me:3000 it does not work any more, do you know how to solve this. I've been try on google but I cannot found any solution.
Thx.