form_forとnested resources

ちょっとメモ.form_for の第1引数にインスタンスオブジェクトを渡す方式だとnested resourcesに対応してないっぽい.

例えばusers/:user_id/projects/:project_idみたいなルートを考える.

map.resources :users do |user|
  user.resources :projects
end

./script/generate scaffoldで生成されたビューはform_forにインスタンスを渡す方式を取っているのでそのままだとエラーが起きる.

  1) Error:
test_should_create_project(ProjectsControllerTest):
ActionView::TemplateError: undefined method `projects_path' for #<ActionView::Ba
se:0x2622080>
    On line #3 of projects/new.html.erb

    1: <h1>New project</h1>
    2: 
    3: <% form_for(@project) do |f| %>
    4:   <%= f.error_messages %>
    5: 
    6:   <p>

接点の無い女の子と99%の確率で会話できる医学的に証明された方法

reddit womenから.How do I strike up a conversation with a girl I've never met? : women

紙をはさむフォルダーを用意します.
インターネットで難しそうな数式を見つけて印刷します.
その紙を他の書類と一緒にフォルダーに入れます.
「拾われた方は○○にご連絡下さい.TEL: ○○○ー○○……」のメモも忘れずに.
次にその女の子を見かけたら彼女の見えないところで髪をグシャグシャにして服をちょっと破きます.
さぁ,彼女の方に走っていき,彼女にフォルダーを押し付けてこう言いましょう.
「ヤツらにこれを渡しちゃ駄目だ!すべて君にかかってる!」
そうしたら急いで逃げましょう.後は彼女が電話をくれるのを待って……ビンゴ!!

ちょっと文房具屋に行ってきます.

restful_authenticationにopen_id_authenticationを組み合わせるスマートな方法

Rails2.xの代表的なユーザ認証pluginのrestful_authenticationと,OpenID認証のpluginであるopen_id_authenticationとを組み合わせる方法の説明.

restfull_authenticationのジェネレータを使って生成したコントローラに対して,open_id_authenticationのREADMEにあるような内容を単純に付け足しただけだと問題が起きる.SessionsControllerのnewメソッドで取得したページにOpenIDのURLを入力して送信するとcreateメソッドが呼ばれる.createメソッドはクライアントをOpenIDプロバイダのページに遷移させる.そこで認証すると,再びcreateメソッドが呼ばれる.このときに,以下のような例外が発生する.

ActionController::InvalidAuthenticityToken in SessionsController#create

この問題はOpenIDプロバイダからSessionsControllerのcreateメソッドに遷移するときのパラメタにauthenticity_tokenが含まれていないために起こる.ApplicationControllerでprotect_from_forgeryを宣言していると,getを除くメソッドの呼び出し時にパラメタ中のauthenticity_tokenをチェックし,一致しなければ上記の例外が発生する.少し前のバージョンのopen_id_authenticationだとこの遷移を処理するのはopen_id_completeという名前付きルートに対するgetメソッドだったので問題が起きなかったようだ.

OpenIDプロバイダから戻ってくるときのパラメタにauthenticity_tokenは含まれていないが,open_id_complete="1"というキーと値のペアが含まれる.これを利用して,パラメタにopen_id_complete="1"が含まれるcreateメソッドの呼び出しのときにはauthenticity_tokenのチェックをしないという風に変更してやればよい.具体的にはprotect_from_forgeryでbefore_filterとして宣言されているverify_authenticity_tokenというメソッドを以下のようにオーバーライドする.

class SessionsController < ApplicationController

  protected

  # Exclude OpenID completion requests from protect_from_forgery
  def verify_authenticity_token
    return ('create' == params[:action] && '1' == params[:open_id_complete]) || super
  end

end

シングルトン・リソースについて

リソースの集合ではないシングルトン・リソース/somethingをroutes.rbで以下のように宣言したとき,

map.resource :something

実際にリクエストをハンドルするコントローラはデフォルトでSomethingsController(複数形).
rake routesするとどうマップされてるか一目瞭然.

% rake routes
(in /Somewhere)
                         POST   /something                       {:action=>"create", :controller=>"somethings"}
                         POST   /something.:format               {:action=>"create", :controller=>"somethings"}
           new_something GET    /something/new                   {:action=>"new", :controller=>"somethings"}
 formatted_new_something GET    /something/new.:format           {:action=>"new", :controller=>"somethings"}
          edit_something GET    /something/edit                  {:action=>"edit", :controller=>"somethings"}
formatted_edit_something GET    /something/edit.:format          {:action=>"edit", :controller=>"somethings"}
               something GET    /something                       {:action=>"show", :controller=>"somethings"}
     formatted_something GET    /something.:format               {:action=>"show", :controller=>"somethings"}
                         PUT    /something                       {:action=>"update", :controller=>"somethings"}
                         PUT    /something.:format               {:action=>"update", :controller=>"somethings"}
                         DELETE /something                       {:action=>"destroy", :controller=>"somethings"}
                         DELETE /something.:format               {:action=>"destroy", :controller=>"somethings"}
                                /:controller/:action/:id         
                                /:controller/:action/:id.:format 

map.resourcesとは違い,indexのルートは作られない.http://api.rubyonrails.com/classes/ActionController/Resources.html#M000697を参照.

土曜に買った本

紀伊国屋で洋書が半額だったので,つい5冊も買ってしまった.

Steve Wozniackの自叙伝.

I, Woz

I, Woz

有名なSF.

2001: a Space Odyssey (Space Odyssey Series)

2001: a Space Odyssey (Space Odyssey Series)

時間のトリセツ.

Time: A User's Guide

Time: A User's Guide

以下2冊は絵本.

自殺するウサギの本.

The Bumper Book of Bunny Suicides

The Bumper Book of Bunny Suicides

Edward Goreyの本.

The Doubtful Guest

The Doubtful Guest

がんばれ

progredditからOur Top 5 Agile Mistakes : programming.リンク先の記事を読む前にコメントのやり取りが面白かったので脊髄反射

pointer2void -1ポイント 4時間 前[-]

Agile is a mistake in the first place.
そもそもアジャイルが間違いなんだよ.

というコメントに対してのコメント.

_ak 2ポイント 4時間 前[-]

Good luck with your death marches.
デスマーチ頑張れ.