!railsもっかい!
{{link /diary/20130211 /diary/20130211}}を見ながらもう一回挑戦

>> code bash
$ cd imgtwit-r
$ ls
$ bundle init
Writing new Gemfile to /home/ina/data/git/imgtwit-r/Gemfile
$ 
<<

えっと
>> code bash
ina@main:~/data/git/imgtwit-r$ bundle install --path vendor/bundle --without production
The Gemfile specifies no dependencies
Your bundle is complete! It was installed into ./vendor/bundle

<<
こうかね、、

>> code bash
$ cat Gemfile
# A sample Gemfile
source "https://rubygems.org"

gem "rails"
<<
と、これでインストールすればいいのね

>> code bash
$ bundle install --path vendor/bundle --without production
ながながと、、
<<

でこのあとrailsのセットアップか

>> code bash
$ bundle exec rails new . --skip-bundle
       exist  
      create  README.rdoc
      create  Rakefile
      create  config.ru
      create  .gitignore
    conflict  Gemfile
Overwrite /home/ina/data/git/imgtwit-r/Gemfile? (enter "h" for help) [Ynaqdh] Y
       force  Gemfile
      create  app
      create  app/assets/images/rails.png
(略)
<<
Gemfileを強制上書きした
で、もういっかいbundle installすればいいのかな、と その前にGemfileをいじるっぽい

GemfileのDBのところを書きなおす
>> code bash
(略)
group :development, :test do
  gem 'sqlite3'
end

group :production do
  gem 'pg'
end

(略)
<<

でもっかい
>> code bash
$ bundle install --path vendor/bundle --without production
(略)

<<

つぎはJSではまるっぽい、 そして解決策が書いてない、、 とおもったけど動いた! なんで?
>> code bash
$ bundle exec rails s
=> Booting WEBrick
=> Rails 3.2.13 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2013-04-29 23:23:17] INFO  WEBrick 1.3.1
[2013-04-29 23:23:17] INFO  ruby 1.9.3 (2012-12-25) [x86_64-linux]
[2013-04-29 23:23:17] INFO  WEBrick::HTTPServer#start: pid=30158 port=3000

<<

!Twitter認証
このへんを見ながらやってみる
- http://blog.twiwt.org/e/c3afce
- http://kimihito.hatenablog.com/entry/2012/10/28/021610

追記
>> code bash
gem 'omniauth'
<<

で、bundle install

config/initializers/omniauth.rb
>> code ruby
Rails.application.config.middleware.use OmniAuth::Builder do
  provider :twitter, 'CONSUMER_KEY', 'CONSUMER_SECRET'
end
<<

>> code bash
$ bundle exec rails g controller sessions
..../vendor/bundle/ruby/1.9.1/gems/omniauth-1.1.4/lib/omniauth/builder.rb:40:in `rescue in provider': Could not find matching strategy for :twitter. You may need to install an additional gem (such as omniauth-twitter). (LoadError)

<< 

あ omniauthのtwitterをいれてないのかな
>> code bash
gem 'omniauth-twitter'
<<
追記
でbundle install

>> code bash
$ bundle exec rails g controller sessions
      create  app/controllers/sessions_controller.rb
      invoke  erb
      create    app/views/sessions
      invoke  test_unit
      create    test/functional/sessions_controller_test.rb
      invoke  helper
      create    app/helpers/sessions_helper.rb
      invoke    test_unit
      create      test/unit/helpers/sessions_helper_test.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/sessions.js.coffee
      invoke    scss
      create      app/assets/stylesheets/sessions.css.scss
<<
よしっ
app/controllers/sessions_controller.rb
>> code ruby


class SessionsController < ApplicationController
  def callback
    auth = request.env["omniauth.auth"]
    user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth)
    session[:user_id] = user.id
    redirect_to root_url, :notice => "Signed in!"
  end

  def destroy
    session[:user_id] = nil
    redirect_to root_url, :notice => "Signed out!"
  end
end
<<

routes.rb に認証のためのルーティングを記述: なるほど
>> code ruby
match '/auth/:provider/callback' => 'sessions#callback'
match "/signout" => "sessions#destroy", :as => :signout
<<
下の方に足した

>> code bash
$ bundle exec rails g model user
<<
>> code bash
$ vim db/migrate/20130429144406_create_users.rb 
<<

>> code ruby
class CreateUsers < ActiveRecord::Migration
  def up
    create_table :users do |t|
      t.string :provider, :null => false
      t.string :uid, :null => false
      t.string :screen_name, :null => false, :uniq => true
      t.string :name, :null => false

      t.timestamps
     end
  end
  
  def down
    drop_table :users
  end
end

<<
こうかなぁ、、

app/models/user.rb
>> code bash

class User < ActiveRecord::Base
  # attr_accessible :title, :body
  def self.create_with_omniauth(auth)
    create! do |user|
      user.provider = auth["provider"]
      user.uid = auth["uid"]
      user.name = auth["info"]["name"]
      user.screen_name = auth["info"]["nickname"]
    end
  end
end
<<

むぅう あってるか微妙
>> code bash
$ bundle exec rake db:migrate
<<
写し間違えで戸惑う。 できた

app/controllers/application_controller.rb 
>> code ruby
class ApplicationController < ActionController::Base
  protect_from_forgery

  helper_method :current_user
  private

  def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end
end
<<

>> code bash
$ bundle exec rails g controller login index
      create  app/controllers/login_controller.rb
       route  get "login/index"
      invoke  erb
      create    app/views/login
      create    app/views/login/index.html.erb
      invoke  test_unit
      create    test/functional/login_controller_test.rb
      invoke  helper
      create    app/helpers/login_helper.rb
      invoke    test_unit
      create      test/unit/helpers/login_helper_test.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/login.js.coffee
      invoke    scss
      create      app/assets/stylesheets/login.css.scss

<<
もうすこし

app/views/login/index.html.erb
>>
<h1>Login#index</h1>
<p>Find me in app/views/login/index.html.erb</p>

<% if current_user %>
  Welcome <%= current_user.name %>!
  <%= link_to "Sign Out", signout_path %>
<% else %>
  <%= link_to "Sign in with Twitter", "/auth/twitter" %>
<% end %>
<<

config/routes.rb
>> code ruby
root :to => "login#index"
<<
追加した

public/index.html を消す。

>> code bash
$ bundle exec rails s
<<
どうだっと




5643382
wiki
1371358960