パーフェクト Ruby on Rails』を読む前に、Railsについてもう一度全体を見通してみようとして、『HerokuではじめるRailsプログラミング入門
』を購入して読み進めています。記述量が適当で、はまらず進める良書ですねこれは。初心者向けにはハマるところは抑えておくものの、いろいろ書かないほうが親切だなと感じました。
ただ、p.160でフォームからのpostが失敗する事例に遭遇、調べた結果解消したのでメモ的に書いておきます。今回の場合は、SampleControllerのindexメソッドのところに、
skip_before_action :verify_authenticity_token ,:only => [:index]
と記載すれば解消するということ。Railsは2.0からCSRF(クロスサイトリクエストフォージェリ)対策で、エラーが出るようにしているのだとか。詳細は参考サイトをご覧ください。
ドキュメント
skip_before_action(names)
Skip a callback before actions. See #_insert_callbacks for parameter details. Aliased as skip_before_filter.
verify_authenticity_token()
The actual before_action that is used to verify the CSRF token. Don’t override this directly. Provide your own forgery protection strategy instead. If you override, you’ll disable same-origin
<script>
verification.Lean on the protect_from_forgery declaration to mark which actions are due for same-origin request verification. If protect_from_forgery is enabled on an action, this before_action flags its after_action to verify that JavaScript responses are for XHR requests, ensuring they follow the browser’s same-origin policy.
やりながら理解していこう。