rails 3 での csrf 対策まとめ
rails3 だと、csrf トークンを投げずに post リクエストなどを送っても、
WARNING: Can’t verify CSRF token authenticity
が出ると同時にセッションがリセットされるが、POST リクエスト自体は通る。
csrf トークン自体は、form_for などのヘルパーを使ったら自動で埋め込まれる。
自分で form タグを書いた場合は
<%= hidden_field_tag :authenticity_token, form_authenticity_token %>
とテンプレートに書いて csrf トークンを埋め込む。
csrf トークンの検証に失敗した場合にエラーとしたい場合は、
class ApplicationController < ActionController def handle_unverified_request raise ActionController::InvalidAuthenticityToken end end
と handle_unverified_request を上書きして、raise ActionController::InvalidAuthenticityToken などする。
NoMethodError: undefined method 'accept' for nil:NilClass が出た時の対処法
- Gemfile に mysql2 を追加
gem 'mysql2'
- activerecord-mysql2-adapter があればコメントアウト
# gem 'activerecord-mysql2-adapter'
- bundle install
- rails 再起動
Rails のデータベース設定でリモートのDBサーバを指定する
host パラメータを指定する。
development: adapter: mysql2 encoding: utf8 database: blog_development pool: 5 username: root password: host: HOST_NAME
複数列をまとめて align-regexp する方法
以下の elisp で align-regexp-repeated コマンドを定義して、呼び出せば良い。
(defun align-regexp-repeated (start stop regexp) "Like align-regexp, but repeated for multiple columns. See http://www.emacswiki.org/emacs/AlignCommands" (interactive "r\nsAlign regexp: ") (let ((spacing 1) (old-buffer-size (buffer-size))) ;; If our align regexp is just spaces, then we don't need any ;; extra spacing. (when (string-match regexp " ") (setq spacing 0)) (align-regexp start stop ;; add space at beginning of regexp (concat "\\([[:space:]]*\\)" regexp) 1 spacing t) ;; modify stop because align-regexp will add/remove characters (align-regexp start (+ stop (- (buffer-size) old-buffer-size)) ;; add space at end of regexp (concat regexp "\\([[:space:]]*\\)") 1 spacing t)))
rails server 時に `autodetect': Could not find a JavaScript runtime が出たら。
rails server したときに
`autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)
と出た時の対処法(といっても公式ドキュメントにそのまんま書いてあるけど・・・)。
Ruby on Rails Guides: Getting Started with Rails の 4.1 Starting up the Web Server に
Compiling CoffeeScript to JavaScript requires a JavaScript runtime and the absence of a runtime will give you an execjs error. (中略) Rails adds the therubyracer gem to Gemfile in a commented line for new apps and you can uncomment if you need it.
とある通り、 therubyracer を gem でインストールする記述が Gemfile 内でコメントアウトされてるそうなので、コメントアウトを外す。
gem 'therubyracer', :platforms => :ruby
そして bundle install しなおす。
bundle install
再び rails server すれば起動する(はず)。
rails 起動
DB なし起動
in `require': cannot load such file -- bigdecimal (LoadError) というエラーが出る場合
ruby on rails 3 - Difficulty with activesupport when attempting to deploy - Stack Overflow
Gemfile に以下を追加して、 bundle install。
gem 'bigdecimal'