私のRailsアプリで。私はdeviseを使用しています。ユーザーは「ja.myapp.com」、「www.myapp.com」などのいくつかのサブドメインからトップページに入るでしょう。私はプロモーションページ内でのみいくつかのサブドメインを使用しているので、「サインイン」ページに入ると、すべてそれらは「www」サブドメインページにリダイレクトされます。
ユーザーがサインインするときに、ユーザーのprofile_typeが「Student」の場合、ユーザーは「Students'home」にリダイレクトされます。「Teacher」の場合、「Teachers'home」です。
「POW」でテストするとすべて正常に動作しますが、本番サーバーにアップロードすると正常に動作します。ユーザーがサインインすると、すべてのユーザーがTOPページにリダイレクトされます。
アプリをロールバックして再度サインインしてみましたが、同じことが起こりました。これは、heroku証明書を「シングルドメインSSL」から「ワイルドカードサブドメインSSL」にアップロードした後に発生しました。
SSLを単一ドメインに変更した後、これはロールバックされたサイト内では発生しなくなります。
これは「ワイルドカードサブドメインSSL」とデバイスセッションコントローラーが原因で発生したと思いますが、それについてはよくわかりません。
誰かがこの問題を解決するためのアイデアを持っていますか?
以下はコードです。
ルート.rb
root :to => 'students#index', :constraints => lambda { |request| request.env['warden'].user.try(:profile_type) == 'Student' }
root :to => 'teachers#index', :constraints => lambda { |request| request.env['warden'].user.try(:profile_type) == 'Teacher' }
root :to => 'pages#home'
devise_for :users, :controllers => {:sessions => "sessions", :registrations => "registrations", :confirmations => "confirmations"}
config / initializers / session_store.rb
MyAPP::Application.config.session_store :cookie_store, key: '_my_app_secure_session', domain: :all
# coding: utf-8
class SessionsController < Devise::SessionsController
after_filter :clear_sign_signout_flash
def after_sign_in_path_for(resource)
url = root_path
end
def new
@title = I18n.t "sessions.new.title"
super
end
def create
super
end
def destroy
super
end
protected
def clear_sign_signout_flash
if flash.keys.include?(:notice)
flash.delete(:notice)
end
end
end
application_controller
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :set_locale, :www_redirect
private
def www_redirect
#if Rails.env.production
parsed_subdomain = request.subdomains.first
locale = Locale.find_by_subdomain(parsed_subdomain)
if (use_www? && parsed_subdomain != "www") || locale.nil?
redirect_to request.url.sub(parsed_subdomain, "www")
end
#end
end
def use_www?
true
end
def set_locale
if user_signed_in?
if current_user.profile_type == "Teacher"
I18n.locale = I18n.default_locale
else
I18n.locale = current_user.locale.i18n_form.to_sym
end
else
if request.subdomains.first == "www" && cookies[:locale]
I18n.locale = cookies[:locale].to_sym
else
I18n.locale = extract_locale_from_subdomain || I18n.default_locale
cookies[:locale] = {
value: I18n.locale.to_s,
expires: 1.year.from_now,
domain: :all
}
end
end
end
def extract_locale_from_subdomain
parsed_subdomain = request.subdomains.first
locale = Locale.find_by_subdomain(parsed_subdomain)
if locale
I18n.available_locales.include?(locale.i18n_form.to_sym) ? locale.i18n_form.to_sym : nil
else
nil
end
end
end
locale_settings_controller.rb
class LocaleSettingsController < ApplicationController
def switch_language
locale = params[:locale]
locale = "www" if locale == "en"
path = params[:uri]
cookies[:locale] = {
value: locale,
expires: 1.year.from_now,
domain: :all
}
if Rails.env.production?
base_url = "http://" + "#{locale}" + ".myapp.com"
else
base_url = "http://" + "#{locale}" + ".myapp.dev"
end
url = path == "/" ? base_url : base_url + "#{path}"
redirect_to url
end
end
Production.rb
config.force_ssl = true
config / initializers / rack_rewrite.rb
require 'rack/rewrite'
LingualBox::Application.config.middleware.insert_before(Rack::Lock, Rack::Rewrite) do
r301 %r{.*}, 'http://www.myapp.com$&', :if => Proc.new {|rack_env|
rack_env['SERVER_NAME'] == 'myapp.com'
}
end if Rails.env == 'production'