Ruby on Rails


Rails3.1 以降でHTTPSを使う


ActiveRecord

セキュリティ


Hello World

  1. プロジェクト作成
    sudo gem install sqlite3 execjs therubyracer
    
    #デフォルトはsqlite3。それ以外は「rails new hello -d mysql」など
    rails new hello
    
    Enter your password to install the bundled RubyGems to your system: ログインユーザのパスワードを入れる。足りないgemをインストールされる
  2. エラー「/usr/local/lib/ruby/gems/1.9.1/gems/execjs-1.4.0/lib/execjs/runtimes.rb:51:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)」
    • JavaScriptライブラリをインストールすると解決する模様
      vi Gemfile # アプリケーションのルート直下にあるGemfileを編集
      ----
      gem 'execjs'
      gem 'therubyracer'
      ----
      bundle install
  3. デモアプリ用に準備
    cd hello
    
    # Postという名前のモデル、ビュー、コントローラーを作成
    rails generate scaffold Post name:string title:string content:text
    
    # DBを作成
    rake db:create
    
    # テーブルを作成
    rake db:migrate
    
    # 現在のルーティングを確認
    rake routes
        posts GET    /posts(.:format)          posts#index
              POST   /posts(.:format)          posts#create
     new_post GET    /posts/new(.:format)      posts#new
    edit_post GET    /posts/:id/edit(.:format) posts#edit
         post GET    /posts/:id(.:format)      posts#show
              PUT    /posts/:id(.:format)      posts#update
              DELETE /posts/:id(.:format)      posts#destroy
  4. 開発用WEBサーバ(WEBrick)を起動。TCP:3000ポート
    • iptables等でフィルタしている場合は開けておく
      sudo vi /etc/sysconfig/iptables
      ----
      -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3000  -j ACCEPT
      ----
      service iptables reload
    • 起動
      cd hello
      rails server
      
      #中止はCtrl+C
  5. ブラウザで http://192.168.1.x:3000/ を開く
  6. 「About your application’s environment」リンクをクリックすると環境がわかる
  7. 追加したpostsコントローラを見てみる
  8. ブラウザで http://192.168.1.x:3000/posts を開く
  9. Listing postsから追加、編集、削除等々が出来ることを確認
  10. topページのコントローラをpostsコントローラに変更してみる
    # indexページを削除
    rm public/index.html
    
    vi config/routes.rb
    ----
    Hello::Application.routes.draw do
      resources :posts
    
      root :to => "posts#index"
    end
    ----
    rails server
  11. ブラウザで http://192.168.1.x:3000/ を開いて見る。postsコントローラが表示されれば成功

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2018-09-15 (土) 07:31:38