2012-02-14 78 views
0

我试图用色器件user_signed_in?不工作时变化的子域

一些不同的超载色器件会话控制器

class SessionsController < Devise::SessionsController 
    def new 
    super 
    end 

    def create 
    resource = warden.authenticate!(:scope => resource_name, :recall => :failure) 
    return sign_in_and_redirect(resource_name, resource)       
    end 

    def sign_in_and_redirect(resource_or_scope, resource) 
    scope = Devise::Mapping.find_scope!(resource_or_scope) 
    resource ||= resource_or_scope 
    sign_in(scope, resource) unless warden.user(scope) == resource 
    subdomain_name = current_user.account.subdomain_name 
    if !current_account 
     redirect_to root_url(:host => "#{subdomain_name}" + "." + "#{request.host}") 
    else 
     redirect_to root_url 
    end 
    end 
end 

我的应用程序控制器是:

class ApplicationController < ActionController::Base 
    include UrlHelper 
    protect_from_forgery 
    helper_method :is_root_domain? 
    before_filter :current_account 

    def is_root_domain? 
    # return true if there is no subdomain 
    puts "++++++++++++++++++++++++++#{request.host_with_port}" 
    (request.subdomains(0).present? && request.subdomains(0) != "www") ? false : true 
    # request.subdomains.first.present? && request.subdomains.first != "www" 
    end 

    def current_account 
    # If subdomain is present, returns the account, else nil 
    if !is_root_domain? 
     @current_account ||= Account.find_by_subdomain_name(request.subdomains(0))  
     if @current_account.nil? 
     flash[:notice] = "There is no Account with this subdomain" 
     redirect_to root_url(:account => false) 
     end 
    else 
     @current_account = nil 
    end 
    @current_account 
    end 

end 

而我的应用程序布局是:

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Subdomain</title> 
    <%= stylesheet_link_tag "application" %> 
    <%= javascript_include_tag "application" %> 
    <%= csrf_meta_tags %> 
    </head> 
    <body> 
    <div id="container"> 
     <div id="header"> 
     <% if is_root_domain? %> 
      <h1>Sub<span class="off">Domain</span></h1> 
     <% else %> 
      <h1><%= @current_account.name %></h1> 
     <% end %> 
     </div> 
     <!--<div><%#=flash[:notice]%> <%#=flash[:alert]%></div>--> 
     <div id="menu"> 
     <ul> 
      <% if user_signed_in? %> 
      <li class="menuitem"><a>Signed in as : <%= current_user.email %></a></li> 
      <li class="menuitem"><%= link_to "Sign out", destroy_user_session_path, :method => :delete %></li> 
      <% end %> 
      <% if !user_signed_in? %> 
      <% if is_root_domain? %> 
       <li class="menuitem"><a href="/">Home</a></li> 
       <li class="menuitem"><a href="#">About</a></li> 
       <li class="menuitem"><a href="#">Products</a></li> 
       <li class="menuitem"><a href="#">Services</a></li> 
       <li class="menuitem"><a href="#">Design</a></li> 
       <li class="menuitem"><a href="#">Contact</a></li> 
       <li class="menuitem"><%= link_to "Sign up", new_user_registration_path %></li> 
       <li class="menuitem"><%= link_to "Sign in", new_user_session_path %></li> 
      <% else %> 
       <li class="menuitem"><%= link_to "Sign in", new_user_session_path %></li> 
      <% end %> 
      <% end %> 
     </ul> 

     </div> 

     <% if user_signed_in? %> 
     <div id="leftmenu"> 

      <div id="leftmenu_top"></div> 

      <div id="leftmenu_main"> 

      <h3>Links</h3> 

      <ul> 
       <li><a href="#">Ruby</a></li> 
       <li><a href="#">PHP</a></li> 
       <li><a href="#">Ajax</a></li> 
       <li><a href="#">jQuery</a></li> 
       <li><a href="#">Web design</a></li> 
       <li><a href="#">Web Programming</a></li> 
       <li><a href="#">RAILS</a></li> 
       <li><a href="#">Internet Marketing</a></li> 
      </ul> 
      </div> 


      <div id="leftmenu_bottom"></div> 
     </div> 
     <% end %> 




     <div id="content"> 


     <div id="content_top"></div> 
     <div id="content_main"> 
      <%= yield %> 
     </div> 
     <div id="content_bottom"></div> 
     </div> 
    </div> 
    </body> 
</html> 

现在,当我试图登录,从根指本地主机重定向到subdomain.localhost 但不能签到为该域

,如果我从subdomain.localhost删除子域:3000它显示在他的网页 即用户的用户已经登录

user_signed_in?不工作的子域

如果我从一个子域名登录,那么所有工作正常。 但是,当我试图从根域签到我得到了错误

有人请帮我

在此先感谢

Kausik

+0

你有看看用于会话的cookie吗?它也适用于子域吗? – phoet 2012-02-14 19:20:48

回答

0

创建会话表“轨生成session_migration”

exampleapp::Application.config.session_store :active_record_store, :domain => :all