参考にしたのは下記サイト
http://kray.jp/blog/twitter_service_in_1hours/
記事はRuby1.8とRails2ベースなのでちょいちょい違う部分もあったのでメモ
<開発前準備>
1.プロジェクトの作成
rails new twitter_helloworld
2.Gitリポジトリ初期化
cd twitter_helloworld
git init
3.バージョン管理をしたくないファイルを指定
vi .gitignore
Macでできるファイル外しておきます。
.DS_Storelog/*.log
tmp/**/*
db/*.sqlite3
db/schema.rb
4.Herokuにプロジェクトを登録
heroku create twitter-helloworld
※プロジェクト名は変更必要有り。同じ名前はHeroku上ではつけられない
5.Twitterアプリとして登録
http://dev.twitter.com/apps/new
・ コールバックURL:http://twitter-helloworld.heroku.com/oauth_callback
・Default Access type:Read & Write
を設定します。
コールバックURLは、自分のサービスURLに「/oauth_callback」を追加した
ものを設定します。
発行された「Consumer key」と「Consumer secret」は後ほど使います。
<開発>
1.public/index.htmlを削除する
rm public/index.html
2.twitter-authインストール
1)Gemfileに下記追加
vi Gemfile
gem ‘twitter-auth’, :require => ‘twitter_auth/engine’, :git => ‘git://github.com/rokudenashi/twitter-auth.git’, :branch => ‘rails_3′
gem ‘json’
2)インストール
bundle install
rails generate twitter_auth
rake db:migrate
generate controller messages index
3)routes.rb編集
vi config/routes.rb
以下追加
# ここから追加
map.resources :messages,:only => [:index, :create]
map.root :controller => ‘messages’, :action => ‘index’
# ここまで追加
4)messages_controller.rb編集
vi app/controllers/messages_controller.rb
以下追加
class MessagesController < ApplicationController
def index
end
# ここから追加
def create
if current_user.twitter.post('/statuses/update.json', :status => “偉大なるHelloWorld”)
flash[:success] = “おめでとう!偉大なるHelloWorldは成功した。”
redirect_to root_path
else
flash[:error] = “残念だが、偉大なるHelloWorldは失敗に終わった。”
render :action => ‘index’
end
end
# ここまで追加
end
5)index.html.erbを編集
vi app/views/messages/index.html.erb
#-*- coding: utf-8 -*-
<% if logged_in? %>
<% form_tag messages_path do %>
<%= submit_tag '偉大なるHelloWorldをツイートする' %>
<% end %>
<% else %>
<%= link_to 'ログインする', login_path %>
<% end %>
6)application.html.erb
vi app/views/layouts/application.html.erb
以下のように編集
<%- flash.each do |name, msg| -%> <%= msg %><%- end -%><%= yield %>
<公開する>
git add .
git commit -m ‘finish!’
git push heroku master
heroku db:push sqlite://db/development.sqlite3 –app twitter20110818
ボタンおしたらつぶやくだけのアプリですがでけたよー
http://twitter20110818.heroku.com/
参照サイト
http://kray.jp/blog/twitter_service_in_1hours/
http://d.hatena.ne.jp/rokudenashi/20100916/1284657458












