Railsでのhttp/https切り換えはには実行環境に依存する部分もある
現在、開発しているWebアプリではプライバシーを扱う一部のページのみ httpsで実行し、それ以外の大部分のページは httpなので、httpからhttpsへの遷移や httpsからhttpへの遷移があります。
Ruby on Railsでは http から https へ遷移する リンクやは下のように
<% link_to :action => 'login', :controller => 'members', :only_path => false, :protocol => 'https' %>
また、https から http へ遷移は
<% link_to :action => 'logout', :controller => 'members', :only_path => false, :protocol => 'http' %>
と書きます。form_for でも同様です。
今時のRailsアプリは map.resoures を用いたRESTfulアプリになっているので members_login_url などのヘルパーメソッドを使ってURLを指定する場合が多いと思いますが、その場合は
<% link_to members_login_url(:protocol => 'https') %>
と書くと https ページに遷移できます。
さて今回、開発していてテスト環境では
<% link_to members_logout_url %>
と書くと https から http ページに遷移できたので、そのまま本番環境(まだ運用されてないのでステージング環境?)で動かしてみましたが、
なぜか上の書き方では https から http に切り替わりませんでした。
いろいろと試してみたところ、開発環境の mongrel + apache mod_porxy と 本番環境 apache + passnger の違いのようでした。
対処として、https から http へ遷移も protocol => 'http'を省略しないことです (無精するなです ^^;)
<% link_to members_logout_url(:protocol => 'http') %>
viewのいたるところに :only_path => false, :protocol => 'https' を書くのは DRY では無いので以下のようなヘルパーを書いて使っています。
helper
def http_prot(opts = {}) {:only_path => false, :protocol => 'http'}.merge(opts) end def https_prot(opts = {}) {:only_path => false, :protocol => 'https'}.merge(opts) end
view
<% link_to members_login_url(https_proto) %> <% link_to members_login_url(https_proto(:sw => 1)) %> <% link_to https_proto(:action => 'login', :controller => 'members') %>
@ITに掲載したGaucheでメタプログラミングの正誤表
以前 @IT に書いた Gaucheでメタプログラミング ですが、オレンジニュースの角田さんが間違いを見つけてくれました ^^); ありがとうございます !!
間違いは現在修正されています
第3回 GaucheでRDBプログラミングの3ページ
最後の実行結果は以下が正しいです。
(ref yamada 'weight)の結果は 60 ではなく 62 です。
gosh> (define yamada (make <body> :height 170 :weight 60)) yamada gosh> (slot-ref yamada 'weight) 60 gosh> (slot-set! yamada 'weight 62) #<undef> gosh> (slot-ref yamada 'weight) 62 gosh> (ref yamada 'weight) 62 gosh> (set! (ref yamada 'weight) 63) #<undef> gosh> (weight-of yamada) 63 gosh> (set! (weight-of yamada) 66) #<undef> gosh> (bmi yamada) 22.837370242214536
第4回 Gaucheでテンプレートエンジンを作るの2ページ
最初のコードは正しくは以下です。
display関数に指定されていた *p* を消して下さい。またtemplate->s-exp関数定義の最後の閉じカッコが足りませんでした ^^);
(define (template->s-exp templ) (define (quote-display m) (format "(display ~s)" (m 1))) (if (not (#/<%/ templ)) templ (let* ((s (regexp-replace-all* templ #/<%=(.*?)%>/ "<% (display \\1) %>" #/%>(.*?)<%/ quote-display)) (str-s-exp (regexp-replace* s #/^(.*?)<%/ quote-display #/%>(.*?)$/ quote-display))) (read-from-string (string-append "(begin " str-s-exp ")"))))) (define (rendering-template templ) (eval (template->s-exp templ) (interaction-environment)))