Originally code is from 2nd section of http://railstutorial.jp/.