万年素人からHackerへの道

万年素人がHackerになれるまで殴り書きするぜ。

  • ・資産運用おすすめ
    10万円は1000円くらい利益
    資産運用ブログ アセマネ
    • ・寄付お願いします
      YENTEN:YYzNPzdsZWqr5THWAdMrKDj7GT8ietDc2W
      BitZenny:ZfpUbVya8MWQkjjGJMjA7P9pPkqaLnwPWH
      c0ban:8KG95GXdEquNpPW8xJAJf7nn5kbimQ5wj1
      Skycoin:KMqcn7x8REwwzMHPi9fV9fbNwdofYAWKRo

    認証を自前で
    →Deviseが分かりにくくて使えない。せっかくのモジュールなのに使いづらい。
    だから自前で!

    http://ja.asciicasts.com/episodes/250-authentication-from-scratch

    でも注意。間違ってる。
    「先ほど作成したUserモデルには属性としてpasswordと password_confirmationを持っていません。その代わりにUserモデル内にこれらを処理するアクセサメソッドを作ります。」
    なんてあるけど、そんな文章に気づくわけ無いじゃん
    ・models/user.rb

    class User < ActiveRecord::Base
      attr_accessible :email, :password, :password_confirmation #日本語版はここが抜けてる!!!
      
      attr_accessor :password
      before_save :encrypt_password
      
      validates_confirmation_of :password
      validates_presence_of :password, :on => :create
      validates_presence_of :email
      validates_uniqueness_of :email
      
      def self.authenticate(email, password)
        user = find_by_email(email)
        if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
          user
        else
          nil
        end
      end
      
      def encrypt_password
        if password.present? #改行注意!!!
          self.password_salt = BCrypt::Engine.generate_salt
          self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
        end
      end
    end
    

    ・app/models/user.rb

    endの数が日本語版は間違ってるので注意!!!
    def self.authenticate(email, password)  
      user = find_by_email(email)  
      if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)  #改行注意
        user  
      else  
        nil  
      end  
    end  #注意
    


    devise
    使い方(英語)
    https://github.com/plataformatec/devise/wiki

    カスタマイズ
    http://gendosu.jp/?p=420

    ・日本語化
    http://memo.yomukaku.net/entries/129
    ディレクトリを先に作る

    mkdir app/config
    mkdir app/config/locales/
    Missing host to link to! Please provide :host parameter or set default_url_options[:host]

    どこで設定だ??
    ・config/environments/development.rb
    これはうまくいかないと思う おそらくメールサーバを自前でやる??

      config.action_mailer.default_url_options = {
      :host => '127.0.0.1',
      :port => 3000
    }
    

    このサイトはいい
    http://d.hatena.ne.jp/babie/20100807/1281141307
    →config/environments/development.rb にGmail設定をする

    ■Devise使うときに「rake db:migrate」でエラー分からない(rails3)

    You have already activated rake 0.9.0, but your Gemfile requires rake 0.8.7. Consider using bundle exec.

    わからない
    Gemfileを

    gem 'rake', '0.9.0'

    rake aborted!ができん
    http://labs.appshelf.info/2011/05/21/238/
    ※config/routes.rb のアプリケーション名を参考にする


    ログイン関連 Rails
    この2つのサイトがいい
    その1
    http://www.oiax.jp/rails/rails3/authentication1.html
    その2
    http://www.oiax.jp/rails/rails3/authentication2.html

    注意:taskのコントローラなどは昔の記事なので読み替えて読むべき。config/routes.rb なども

    DEPRECATION WARNINGがうざい

    bunderとwill_paginateをインストールしたら下記の警告がコンソールへ!
    「DEPRECATION WARNING: railtie_name is deprecated and has no effect.」

    このサイトで「http://www.oiax.jp/rails/rails3/authentication1.html
    will_paginateは'3.0.pre'がダメっぽい。

    Gemfileを

    gem 'will_paginate', '3.0.pre'
    

    から

    gem 'will_paginate', '3.0.pre2'
    

    に変更

    bundle install


    ■localhosr:3000だけでのリダイレクトの注意 Rails3
    config/routes.rb を修正します。
    ・config/routes.rb

    Hoge::Application.routes.draw do  # Hogeはアプリの名前
      root :to => 'hoge#index'  # hogeはコントローラ名
      # 省略
    end
    

    これだけだとリダイレクトされない。
    「public/index.html」を削除します!!!