ruby-on-railsdevisesubdomainrails-flash

Rails 3 notices


Using Rails 3.0.3 in combination with gems like devise I don't get any flash messages. Now I mention devise, because it controls the cookies used to authenticate me.

Now the Rails application is a bit special since its using sub-domains. What they do is tell the application which company a user is browsing. An example: mycompany.theapp.com/projects/3/ <- Project 3 of company "mycompany".
When a user logs in he/she is directed to theapp.com/overview <- non subdomain
Every notice there does show, so why not on sub-domain pages?

So this is the code used on sub-domain pages. Exactly the same as one would put it on non-sub-domain pages. The code in the controller:

def update
  redirect_to [@project], :notice => "Project #{@project.name} updated."
end

The layouts/application.html.erb

<% flash.each do |type, message| %>
  <%= content_tag :div, message, :class => "flash #{type}" %>
<% end %>

I also added the following, trying to figure out what is happening:

flash.to_yaml
# Result: --- !map:ActionDispatch::Flash::FlashHash {} 

Since apparently devise uses flash[:notice] I also tried that (because that was working for devise).

flash[:notice] = "Project #{@project.name} updated."
# Result: --- !map:ActionDispatch::Flash::FlashHash {} 

Now do the notices get send by cookies or session data that isn't transferred well to sub-domains? Because the edit form of the project is on mycompany.theapp.com/projects/3/edit/
And it sends me to mycompany.theapp.com/projects/3/ (without notice)

It happens on Ubuntu 10.10 with "rails server", passenger using nginx and even on a Mac machine (but someone else tested that).

Anyone care to guess?


Solution

  • Apparently there wasn't really something wrong with the configuration. We deployed it on a server real quick, production mode, and it worked, much to our surprise. The notices were showing up across all domains.
    It's something with localhost domains, I was using lvh.me, that Rails doesn't quite get.

    The hint was in a (recent) comment on a railscast:

    http://railscasts.com/episodes/221-subdomains-in-rails-3?view=comments#comment_146276 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 blog.plataformatec.com.br/2009/12/subdomains-and-sessions-to-the-rescue/

    @David Sulc, the users were logged in.