当用户登录到我的example.com站点时,我希望他在访问something.example.com时也能登录。我如何才能做到这一点呢?(如果相关,我使用subdomain-fu )
发布于 2010-07-02 01:20:56
修复方法是将此代码添加到production.rb中
if config.action_controller.session
config.action_controller.session[:domain] = '.your-site.com'
else
config.action_controller.session = { :domain => '.your-site.com' }
end我仍然不能让它在localhost:3000的开发中工作,但无所谓
发布于 2011-01-20 21:41:21
当然,您可以在/etc/hosts中"127.0.0.1 localhost“之后添加以下代码行
127.0.0.1 localhost.com
127.0.0.1 sub.localhost.com然后编辑您的环境/development.rb并添加
config.action_controller.session = { :domain => '.localhost.com' }从现在开始,使用http://localhost.com:3000或相同但带有子域的子域来访问您的应用程序。
更新哦,这是对Horace Loeb的回答
发布于 2012-11-15 05:31:48
对于Rails3,上面的代码将引发NoMethodError
undefined method `session=' for ActionController::Base:Class因此,对于Rails3,您不应该更改您的环境配置,而应该将您的app/config/initializers/session_store.rb设置为:
YourAppName::Application.config.session_store :active_record_store,
{:key => '_your_namespace_session', :domain => '.yourdomain.com'}另外,在更改初始化器之后,您需要重新启动you服务器才能应用初始化器。
请注意,在代码更新之前登录的用户在此之后将无法注销,因为默认的注销操作如下所示:
destroy
current_user_session.destroy
flash[:notice] = "You have been logged out"
redirect_to root_path
end是不够的-默认情况下,它不会删除为非通配域yourdomain.com设置的user_credentials cookie。因此,您应该将cookies.delete :user_credentials添加到销毁操作中,使其如下所示:
destroy
current_user_session.destroy
cookies.delete :user_credentials
flash[:notice] = "You have been logged out"
redirect_to root_path
end这很奇怪,但它应该在销毁用户会话之后添加,尽管此时使用了cookies[:user_credentials].is_nil? == true。此外,还存在一个问题,在用户注销然后登录之后,在destroy操作中使用cookies.delete :user_credentials也会使用户无法注销,因此应该将其删除。有人有解决这个问题的办法吗?
更新。最后,我想到了这一点--我通过迁移向用户模型添加了一个布尔标志:
class AddReloginedToUsers < ActiveRecord::Migration
def change
add_column :users, :relogined, :boolean, :default => false
end
end并以这种方式更改了销毁操作:
def destroy
current_user_session.destroy
if !current_user.relogined
current_user.relogined = true
current_user.save
cookies.delete(:user_credentials)
end
session = nil
flash[:notice] = "You have been logged out"
redirect_to root_path
end现在一切都像预期的那样工作,尽管这不是一个非常好的解决方案。如果有人提供更聪明的东西,我会很高兴的。
https://stackoverflow.com/questions/3144696
复制相似问题