Advertisement
kinjo

rails3.2 misc

May 11th, 2012
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.89 KB | None | 0 0
  1. rails3.2 misc
  2. =============
  3.  
  4. Author: @libkinjo
  5. Date: 2012-05-12 12:39:41 JST
  6.  
  7.  
  8. Table of Contents
  9. =================
  10. 1 rails プロジェクト開始直後(rails new の直後)にやること
  11. 2 rails3.2 をインストールする前にインストールしておくとよい rubygems
  12. 3 ruby 1.9.3 で ruby-debug19 をインストールする
  13. 4 rails3.2 インストール
  14. 5 i18n_generators 日本語化 - validation メッセージとモデルのフィールド名
  15. 6 i18n_generators 日本語化 - 日本語化されたモデルのフィールド名を取得
  16. 7 coffeescript を使う
  17. 8 coffeescript ではなく javascript ファイル(.js) を使いたい場合
  18. 9 posts.js.coffee と posts.js が両方存在する場合の動作 => 期待通りの動作にならない
  19. 10 sass http://sass-lang.com/
  20. 11 情報(ガイド) http://guides.rubyonrails.org/
  21. 12 ruby-debug エラー: ruby_debug.so: undefined symbol: ruby_current_thread - (LoadError)
  22. 13 file 'lib' not found と表示されてインストールできない
  23. 14 Could not find a JavaScript runtime と表示されて rails s できない
  24.  
  25.  
  26. 1 rails プロジェクト開始直後(rails new の直後)にやること
  27. ---------------------------------------------------------
  28.  
  29. git を使用するため、 git init で初期化し、 git commit で初期コミットしておく。
  30.  
  31.  
  32.  
  33. git init
  34. git add .
  35. git commit -m 'init'
  36.  
  37.  
  38. therubyracer 有効化する。 (Gemfile の以下の行をコメントインし bundle install を実行する。)
  39.  
  40.  
  41.  
  42. gem 'therubyracer', :platform => :ruby
  43.  
  44.  
  45. ruby-debug19 有効化する。 (Gemfile の以下の行をコメントインし bundle install を実行する。)
  46.  
  47.  
  48.  
  49. gem 'ruby-debug19', :require => 'ruby-debug'
  50.  
  51.  
  52. pry 関連の設定。
  53.  
  54. Gemfile の group :development, :test ブロック中に、以下のように 設定する。 (Gemfile 編集後に bundle install を実行する。)
  55.  
  56.  
  57.  
  58. group :development, :test do
  59. gem 'pry-rails'
  60. gem 'pry-doc'
  61. end
  62.  
  63.  
  64.  
  65.  
  66. bundle install
  67.  
  68.  
  69. ruby-debug のために、以下も実行しておく。(必要ないかも?)
  70.  
  71.  
  72.  
  73. bundle update ruby-debug-base19
  74.  
  75.  
  76. ここまで git commit する。
  77.  
  78.  
  79.  
  80. git add .
  81. git commit -m 'install pry'
  82.  
  83.  
  84. haml 関連の設定。
  85.  
  86. 以下を実行して Gemfile を編集する。(Gemfile 編集後に bundle install を実行する。)
  87.  
  88.  
  89.  
  90. echo "gem 'haml-rails'" >> Gemfile
  91. echo "gem 'coffee-filter'" >> Gemfile
  92.  
  93.  
  94. html2haml を使うため、以下を実行して haml をインストールする。
  95.  
  96.  
  97.  
  98. gem install haml --no-ri --no-rdoc
  99. gem install hpricot --no-ri --no-rdoc
  100. gem install ruby_parser --no-ri --no-rdoc
  101.  
  102.  
  103. rbenv rehash を実行し html2haml を使えるようにする。
  104.  
  105.  
  106.  
  107. rbenv rehash
  108.  
  109.  
  110. html2haml を実行し application.html.erb を application.html.haml へ変換する。
  111.  
  112.  
  113.  
  114. html2haml -e app/views/layouts/application.html.erb app/views/layouts/application.html.haml
  115. git add .
  116. git rm app/views/layouts/application.html.erb
  117. git commit -m 'move app/views/layouts/application.html.erb to application.html.haml'
  118.  
  119.  
  120. rspec, spork ([https://github.com/sporkrb/spork-rails]) 関連の設定。
  121.  
  122. 以下を実行して Gemfile を編集する。
  123.  
  124. group :development, :test が既にあればブロック中に以下を追加する。
  125. group :development, :test がなければ group を追加し、そのブロック中に以下を追加する。
  126. (Gemfile 編集後に bundle install を実行する。)
  127.  
  128.  
  129.  
  130. gem 'rspec-rails'
  131. gem 'spork-rails'
  132.  
  133.  
  134. rspec 関連の設定ファイルを生成する。
  135.  
  136.  
  137.  
  138. rails g rspec:install
  139.  
  140.  
  141. spork を使用可能とするために rbenv rehash を実行する。
  142.  
  143.  
  144.  
  145. rbenv rehash
  146.  
  147.  
  148. spork を実行し spork の設定ファイルを生成する。
  149.  
  150.  
  151.  
  152. spork rspec --bootstrap
  153.  
  154.  
  155. spec/spec_helper.rb を編集する。
  156. (spec_helper.rb にある # This file is copied to spec/ when you run 'rails generate rspec:install'
  157. 行より以下のコードを Spork.prefork ブロック中に移動する)
  158.  
  159. また Spork.each_run ブロック中に以下を追加する。
  160.  
  161.  
  162.  
  163. # http://nerian.es/2011/02/28/RPS-3-spork_and_mongoid.html
  164. load "#{ Rails.root}/config/routes.rb"
  165. Dir["#{Rails.root}/app/**/*.rb"].each { |f| load f }
  166.  
  167.  
  168. spec/spec_helper.rb のファイル先頭付近にある以下の require 行をコメントインする。
  169. (アンコメントすると rspec 内での debugger 呼び出しでブレークできなくなる問題があった
  170. 2012-05-03 木。現状ではコメントアウトしたままの方がいいかもしれない)
  171.  
  172.  
  173.  
  174. require 'spork/ext/ruby-debug'
  175.  
  176.  
  177. ここまで git commit する。
  178.  
  179.  
  180.  
  181. echo "TAGS" >> .gitignore
  182. echo "tags" >> .gitignore
  183. git add .
  184. git commit -m 'install rspec and spork'
  185.  
  186.  
  187. factory_girl 関連の設定。
  188.  
  189. Gemfile の group :test, :development ブロック中に gem 'factory_girl_rails' を追加する。
  190. (Gemfile 編集後に bundle install を実行する。)
  191.  
  192.  
  193.  
  194. gem 'factory_girl_rails'
  195.  
  196.  
  197. spec/spec_helper.rb の Spork.each_run ブロック中に以下を追加する。
  198.  
  199.  
  200.  
  201. FactoryGirl.reload
  202.  
  203.  
  204. ここまで git commit する。
  205.  
  206.  
  207.  
  208. git add .
  209. git commit -m 'setup factory_girl'
  210.  
  211.  
  212. 2 rails3.2 をインストールする前にインストールしておくとよい rubygems
  213. ---------------------------------------------------------------------
  214.  
  215. pry は ruby の調査に役立つ。
  216.  
  217.  
  218.  
  219. gem install pry --no-ri --no-rdoc
  220. gem install pry-doc --no-ri --no-rdoc
  221.  
  222.  
  223. haml を使うとき html2haml が役立つ。
  224.  
  225.  
  226.  
  227. gem install haml --no-ri --no-rdoc
  228. gem install hpricot --no-ri --no-rdoc
  229. gem install ruby_parser --no-ri --no-rdoc
  230.  
  231.  
  232. ruby-debug19 をインストールする。
  233.  
  234. 3 ruby 1.9.3 で ruby-debug19 をインストールする
  235. ------------------------------------------------
  236.  
  237. gem をとってくる。(ちゃんと取ってこれているか調べる。ファイルが空じゃないかとか)
  238.  
  239.  
  240.  
  241. wget http://rubyforge.org/frs/download.php/75414/linecache19-0.5.13.gem
  242. wget http://rubyforge.org/frs/download.php/75415/ruby-debug-base19-0.11.26.gem
  243. wget http://rubyforge.org/frs/download.php/63094/ruby-debug19-0.11.6.gem
  244. wget http://rubyforge.org/frs/download.php/74596/ruby_core_source-0.1.5.gem
  245.  
  246.  
  247. RVM_SRC をセットする
  248.  
  249.  
  250.  
  251. export RVM_SRC=$HOME/.rbenv/versions/1.9.3-p125
  252.  
  253.  
  254. とってきた gem をインストールする。
  255.  
  256. HTTP PROXY な環境なら /etc/environment に以下を設定する。
  257.  
  258.  
  259.  
  260. HTTP_PROXY="http://proxy.co.jp:8080/"
  261. HTTPS_PROXY="https://proxy.co.jp:8080/"
  262.  
  263.  
  264. gem をインストールする。
  265.  
  266.  
  267.  
  268. gem install ./ruby_core_source-0.1.5.gem -- --with-ruby-include=/$RVM_SRC --no-ri --no-rdoc
  269. gem install ./linecache19-0.5.13.gem -- --with-ruby-include=/$RVM_SRC --no-ri --no-rdoc
  270. gem install ./ruby-debug-base19-0.11.26.gem -- --with-ruby-include=$HOME/.rbenv/versions/1.9.3-p125/include/ruby-1.9.1/ruby-1.9.3-p125 --no-ri --no-rdoc
  271. gem install ./ruby-debug19-0.11.6.gem -- --with-ruby-include=/$RVM_SRC --no-ri --no-rdoc
  272.  
  273.  
  274.  
  275. rails プロジェクトで以下を実行する。
  276.  
  277.  
  278.  
  279. bundle update ruby-debug-base19
  280.  
  281.  
  282. 4 rails3.2 インストール
  283. ------------------------
  284.  
  285.  
  286.  
  287. gem install rails --no-ri --no-rdoc
  288.  
  289.  
  290. rbenv を使っている場合、 gem install した後は rbenv rehash すること。
  291.  
  292. 5 i18n_generators 日本語化 - validation メッセージとモデルのフィールド名
  293. -------------------------------------------------------------------------
  294.  
  295. Gemfile に以下を追加して bundle install を実行する。
  296.  
  297.  
  298.  
  299. gem 'i18n_generators'
  300.  
  301.  
  302. rails のエラーメッセージを日本語化するためのファイルを下記コマンドで生成する。
  303.  
  304.  
  305.  
  306. rails g i18n_locale ja
  307.  
  308.  
  309. config/application.rb を編集する。(以下の行を追加)
  310.  
  311.  
  312.  
  313. config.i18n.default_locale = :ja
  314.  
  315.  
  316. この段階でエラーメッセージは日本語化されるが、モデルのフィールド名などは
  317. 日本語化されない。
  318.  
  319. モデルのフィールド名を日本語化するためのファイルを下記コマンドで生成する。
  320.  
  321.  
  322.  
  323. rails g i18n_translation ja
  324.  
  325.  
  326. config/locales/translation_ja.yml が生成される。
  327. あとは、適宜 config/locales/translation_ja.yml を編集しながら日本語化をすすめていく。
  328.  
  329. 6 i18n_generators 日本語化 - 日本語化されたモデルのフィールド名を取得
  330. ----------------------------------------------------------------------
  331.  
  332.  
  333.  
  334. Model.human_attribute_name(:field)
  335.  
  336.  
  337. 7 coffeescript を使う
  338. ----------------------
  339.  
  340. rails generate scaffold したとき coffeescript の雛形ファイルが生成される。
  341.  
  342. rails g scaffold Post .. を実行した場合は app/assets/javascripts/posts.js.coffee が
  343. 生成される。
  344.  
  345. Post のビューで使用するロジックを app/assets/javascripts/posts.js.coffee に記述する。
  346. 以下例。
  347.  
  348.  
  349.  
  350. # Place all the behaviors and hooks related to the matching controller here.
  351. # All this logic will automatically be available in application.js.
  352. # You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
  353. $ =>
  354. $('a').click =>
  355. alert 'hello'
  356. return false
  357.  
  358.  
  359. 8 coffeescript ではなく javascript ファイル(.js) を使いたい場合
  360. ----------------------------------------------------------------
  361.  
  362. rails generate scaffold したとき coffeescript の雛形ファイルが生成される。
  363.  
  364. 例えば
  365. rails g scaffold Post .. を実行した場合は app/assets/javascripts/posts.js.coffee が
  366. 生成される。
  367.  
  368. この app/assets/javascripts/posts.js.coffee を削除して
  369. app/assets/javascripts/posts.js を作成しなおせば、
  370. app/assets/javascripts/posts.js に
  371. javascript の記述を行える。
  372.  
  373. *.js.coffee と *.js が存在している場合は *.js.coffee が優先して実行される?
  374.  
  375. 9 posts.js.coffee と posts.js が両方存在する場合の動作 => 期待通りの動作にならない
  376. -----------------------------------------------------------------------------------
  377.  
  378. jquery のイベントコールバックは二重実行となることを確認した。
  379.  
  380. あと posts.js は評価されるが posts.js.coffee は評価されないよう。
  381.  
  382. 10 sass [http://sass-lang.com/]
  383. --------------------------------
  384.  
  385. sass の特徴については [http://sass-lang.com/] に掲載されている
  386. Variables, Nesting, Mixins, Selector Inheritance で理解できる。
  387.  
  388. - Variables
  389. - パラメータ値を変数でもてる
  390. - 変数は $ 記号から始まる
  391. - Nesting
  392. - 文字通り入れ子にできる
  393. - Mixins
  394. - @ 記号で Mixins を定義する
  395. - @include で Mixins する
  396. - Selector Inheritance
  397. - @extend で継承する
  398.  
  399. 11 情報(ガイド) [http://guides.rubyonrails.org/]
  400. -------------------------------------------------
  401.  
  402. 12 ruby-debug エラー: ruby_debug.so: undefined symbol: ruby_current_thread - (LoadError)
  403. -----------------------------------------------------------------------------------------
  404.  
  405. 以下のように表示され rails s -u できない
  406.  
  407.  
  408.  
  409. /home/first/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/ruby-debug-base19-0.11.25/lib/ruby-debug-base.rb:1:in `require': /home/first/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/ruby-debug-base19-0.11.25/lib/ruby_debug.so: undefined symbol: ruby_current_thread - /home/first/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/ruby-debug-base19-0.11.25/lib/ruby_debug.so (LoadError)
  410. from /home/first/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/ruby-debug-base19-0.11.25/lib/ruby-debug-base.rb:1:in `<top (required)>'
  411.  
  412.  
  413. 以下を参考にして解決
  414.  
  415. [http://blog.udzura.jp/2012/02/15/ruby-debug19-install-on-ruby193-p0-and-rvm/]
  416.  
  417. 上記を試した記録は次を参照 >>>ruby-debug
  418.  
  419. 13 file 'lib' not found と表示されてインストールできない
  420. ---------------------------------------------------------
  421.  
  422. --no-ri --no-rdoc オプションをつけてインストールする
  423.  
  424. [http://passingloop.tumblr.com/post/10519067297/file-lib-not-found-when-installing-rails-3-1-on-mac-os-x]
  425.  
  426. 14 Could not find a JavaScript runtime と表示されて rails s できない
  427. ---------------------------------------------------------------------
  428.  
  429. 以下のように表示される
  430.  
  431. /home/first/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/execjs-1.3.0/lib/execjs/runtimes.rb:50:in `autodetect': Could not find a JavaScript runtime. See [https://github.com/sstephenson/execjs] for a list of available runtimes. (ExecJS::RuntimeUnavailable)
  432.  
  433. Gemfile にある以下の一行をコメントインする。
  434.  
  435.  
  436.  
  437. gem 'therubyracer', :platform => :ruby
  438.  
  439.  
  440. 最後に bundle install を実行する。
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement