Facebook APIを使ってアプリを作ることになったので、せっかくなので環境にインストールするままで終わっていたRuby on Railsで実装してみようと考えました。
とはいえいきなりスクラッチで作るのはハードル高いので、サンプルコードを探していたところ、githubでコードを公開していらっしゃる方がいました。
https://github.com/banane/sample-koala-rails-app
koalaはRoRのfacebook apiライブラリです。これはぜひ使ってみたい! とゆうことで、forkして実際に使ってみるまでをメモ。Rails3対応だったので、Rails4に対応させてみました。
forkしてみた
画面右上のforkボタンを押すと、自分のアカウントにforkされます。
git cloneしてみる
git clone git@github.com:tomoima525/sample-koala-rails-app.git cd sample-koala-rails-app
FB Developerでアプリケーション登録
この辺はぐぐればすぐ出てきます。ここでは省略。
/etc/hostsでFB Developerで登録したURLをローカルドメインに設定
vi /etc/hosts myapp.com(FB Developerで登録したURL) 127.0.0.1
config / initializers / constants.rb の編集
/config/initializers/constants.rbに、FB Developerで発行されたAPP_IDとAPP_SECRET、/etc/hostsで設定したURLを登録します。
APP_ID= 'xxx' # please change! APP_SECRET= 'xxx' # please change! SITE_URL = 'http://myapp.com:3000'
OATHのcallback設定を変更
ローカルでテストする場合は/app/controllers/home_controller.rbのcallbackにportを入れてあげないと、うまくcallbackしてくれません。 以下のように修正しました。
/app/controllers/home_controller.rb - def index - session[:oauth] = Koala::Facebook::OAuth.new(APP_ID, APP_SECRET, SITE_URL + '/home/callback') + def index + #session information 追加 + session[:oauth] = Koala::Facebook::OAuth.new(APP_ID, APP_SECRET, SITE_URL + ':3000/home/callback')
rails sしてみる
エラーでまくる。そもそもsample-koala-rails-appはRails3で動くアプリなので、Rails4に対応させなくてはいけないのです。 以下対応させるための変更点。
- gemfileの書き換え
#rails -s Could not find gem 'rails (= 3.1.0) ruby' in the gems available on this machine. Run `bundle install` to install missing gems. #bundle install Fetching gem metadata from http://rubygems.org/......... Fetching additional metadata from http://rubygems.org/.. Resolving dependencies... Bundler could not find compatible versions for gem "railties": In Gemfile: coffee-rails (~> 3.1.0) ruby depends on railties (~> 3.1.0.rc1) ruby rails (= 4.0.0) ruby depends on railties (4.0.0)
と出たので、とりあえずgemfileで3.1.0になっているところを全部 4.0.0にしてみる。koalaも最新にしておく。
-gem 'rails', '3.1.0' -gem 'koala', '1.3.0' - gem 'sass-rails', " ~> 3.1.0" - gem 'coffee-rails', "~> 3.1.0" +gem 'rails', '4.0.0' +gem 'koala', "~>1.8.0rc1" + gem 'sass-rails', " ~> 4.0.0" + gem 'coffee-rails', "~> 4.0.0"
- config/environments配下のファイルの置き換え
ふたたびでrails sを実施すると以下メッセージがでる
config.whiny_nils option is deprecated and no longer works
config/environments以下の設定ファイルが古いらしい。
対処方法は http://qiita.com/shu_0115/items/929396bc915db909da26
config/environments配下の各ファイルの中身をRails4で生成されたファイルの中身で差し替える。何のプロジェクトのものでもOKのようです。
- normalize_conditionsのエラー
ようやくいけるかと思って再度rails sをしたところ、またもやエラーが。
# rails s => Booting WEBrick => Rails 4.0.0 application starting in development on http://0.0.0.0:3000 => Run `rails server -h` for more startup options => Ctrl-C to shutdown server Exiting /home/tomoaki/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/routing/mapper.rb:191:in `normalize_conditions!': You should not use the `match` method in your router without specifying an HTTP method. (RuntimeError) If you want to expose your action to both GET and POST, add `via: [:get, :post]` option. If you want to expose your action to GET, use `get` in the router: Instead of: match "controller#action" Do: get "controller#action"
これもrailsの書き方がバージョンで変わったかららしい。
https://github.com/RailsApps/rails-composer/issues/111
routes.rbの matchをgetに直すことでいけます。
/config/routes.rb - match 'home/callback' => 'home#callback' + get 'home/callback' => 'home#callback'
これでようやく動きました。
これ見て勉強しています。Rubyの解説も書いてあるし、実際に作りながら勉強できるので、結構良いです。
改訂新版 基礎Ruby on Rails (IMPRESS KISO SERIES)
- 作者: 株式会社オイアクス,黒田努,佐藤和人
- 出版社/メーカー: インプレスジャパン
- 発売日: 2012/03/05
- メディア: 単行本(ソフトカバー)
- 購入: 1人 クリック: 20回
- この商品を含むブログ (6件) を見る