2011年12月6日火曜日

ActiveAdminをhttpsで使う

Railsの管理画面プラグインActiveAdminで、httpsを使う必要があったので rack_ssl_enforcer というのを使ってみた。

group :production do
gem 'rack-ssl-enforcer', :require => 'rack/ssl-enforcer'
end
view raw Gemfile hosted with ❤ by GitHub
if ENV['RACK_ENV'] == 'production'
AppName::Application.config.middleware.insert_before(ActionDispatch::Cookies, Rack::SslEnforcer, :only => /^\/admin/)
end
2011年12月5日月曜日

deviseでログイン後のリダイレクトはsslを使いたくない場合

deviseを使っていてメールアドレスやパスワードを入力するページは force_ssl をかけていたんだけど、ログイン後のリダイレクトでhttps://hogehoge に飛ばされちゃうと一部の画像とかがssl対応できなかったのでブラウザの判定はイマイチになっちゃう。
そんな時は Devise::SessionsController#create をオーバーライドする

class Users::SessionsController < Devise::SessionsController
# POST /users/sign_in
def create
resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
set_flash_message(:notice, :signed_in) if is_navigational_format?
sign_in(resource_name, resource)
respond_with resource, :location => "http://#{request.host_with_port}#{redirect_location(resource_name, resource)}"
end
end
view raw gistfile1.rb hosted with ❤ by GitHub