railsの基礎
DESCRIPTION
pgcafe で発表した内容ですTRANSCRIPT
![Page 1: Railsの基礎](https://reader031.vdocuments.pub/reader031/viewer/2022020218/55660e3dd8b42aa6628b5389/html5/thumbnails/1.jpg)
Ruby on Rails の基礎@jishiha
13年1月10日木曜日
![Page 2: Railsの基礎](https://reader031.vdocuments.pub/reader031/viewer/2022020218/55660e3dd8b42aa6628b5389/html5/thumbnails/2.jpg)
参考• Ruby on Rails の基礎 byドットインストール http://dotinstall.com/lessons/
basic_rails
• Ruby on Rails をこれから始める人へのおすすめ本やおすすめ情報 http://
blog.champierre.com/963
13年1月10日木曜日
![Page 3: Railsの基礎](https://reader031.vdocuments.pub/reader031/viewer/2022020218/55660e3dd8b42aa6628b5389/html5/thumbnails/3.jpg)
環境構築
• Mac OS X 10.8
• Mac OS X 10.8 Mountain Lion に Ruby on
Rails 環境をセットアップする http://
blog.champierre.com/961
13年1月10日木曜日
![Page 4: Railsの基礎](https://reader031.vdocuments.pub/reader031/viewer/2022020218/55660e3dd8b42aa6628b5389/html5/thumbnails/4.jpg)
MVC?
• Model データ
• View 見た目
• Controller MとVを結ぶ処理
13年1月10日木曜日
![Page 5: Railsの基礎](https://reader031.vdocuments.pub/reader031/viewer/2022020218/55660e3dd8b42aa6628b5389/html5/thumbnails/5.jpg)
新しいプロジェクトの作成
• rails new blog
• echo "rvm use ruby-1.9.3" > blog/.rvmrc
• cd blog
13年1月10日木曜日
![Page 6: Railsの基礎](https://reader031.vdocuments.pub/reader031/viewer/2022020218/55660e3dd8b42aa6628b5389/html5/thumbnails/6.jpg)
ファイル構成
• cd blog
• subl .
13年1月10日木曜日
![Page 7: Railsの基礎](https://reader031.vdocuments.pub/reader031/viewer/2022020218/55660e3dd8b42aa6628b5389/html5/thumbnails/7.jpg)
Web サーバーを起動
• rails s (WEBrick)
• open http://localhost:3000
• 本当は pow のほうがオススメ http://
pow.cx/
13年1月10日木曜日
![Page 8: Railsの基礎](https://reader031.vdocuments.pub/reader031/viewer/2022020218/55660e3dd8b42aa6628b5389/html5/thumbnails/8.jpg)
scaffold
• rails g scaffold Post title:string content:text
• bundle exec rake db:migrate
• open http://localhost:3000/posts
13年1月10日木曜日
![Page 9: Railsの基礎](https://reader031.vdocuments.pub/reader031/viewer/2022020218/55660e3dd8b42aa6628b5389/html5/thumbnails/9.jpg)
DB(sqlite)の確認
• SQLite Manager https://addons.mozilla.org/ja/firefox/addon/sqlite-manager/
13年1月10日木曜日
![Page 10: Railsの基礎](https://reader031.vdocuments.pub/reader031/viewer/2022020218/55660e3dd8b42aa6628b5389/html5/thumbnails/10.jpg)
URLの設定
• config/routes.rb を編集
• root :to => 'posts#index' を追加
13年1月10日木曜日
![Page 11: Railsの基礎](https://reader031.vdocuments.pub/reader031/viewer/2022020218/55660e3dd8b42aa6628b5389/html5/thumbnails/11.jpg)
rails console
• rails c
• >> Post.all
13年1月10日木曜日
![Page 12: Railsの基礎](https://reader031.vdocuments.pub/reader031/viewer/2022020218/55660e3dd8b42aa6628b5389/html5/thumbnails/12.jpg)
Controllerの編集
• respond_to do |format|... は削除
• def create/update/destroy では format.html
{...} の {} 内を残す
13年1月10日木曜日
![Page 13: Railsの基礎](https://reader031.vdocuments.pub/reader031/viewer/2022020218/55660e3dd8b42aa6628b5389/html5/thumbnails/13.jpg)
Viewの確認• app/views/posts/index.html.erb
• ファイル名がアクション名に対応
• @付き変数にアクセスできる
• View の外枠は views/layouts/
application.html.erb で定義
13年1月10日木曜日
![Page 14: Railsの基礎](https://reader031.vdocuments.pub/reader031/viewer/2022020218/55660e3dd8b42aa6628b5389/html5/thumbnails/14.jpg)
Rails と Sinatra の違い
• link_to image_to などのヘルパーが便利
• <%= f.submit '投稿', disable_with: "処理中..." %>
13年1月10日木曜日
![Page 15: Railsの基礎](https://reader031.vdocuments.pub/reader031/viewer/2022020218/55660e3dd8b42aa6628b5389/html5/thumbnails/15.jpg)
Heroku に deploy(1)
• Gemfile の編集
group :development do gem 'sqlite3'end
group :production do gem 'pg'end
13年1月10日木曜日
![Page 16: Railsの基礎](https://reader031.vdocuments.pub/reader031/viewer/2022020218/55660e3dd8b42aa6628b5389/html5/thumbnails/16.jpg)
Heroku に deploy(2)
• bundle install
• git init
• git add .
• git commit -a
• /usr/local/heroku/bin/heroku create
13年1月10日木曜日
![Page 17: Railsの基礎](https://reader031.vdocuments.pub/reader031/viewer/2022020218/55660e3dd8b42aa6628b5389/html5/thumbnails/17.jpg)
Heroku に deploy(3)
• git push heroku master
• /usr/local/heroku/bin/heroku run rake db:migrate
• /usr/local/heroku/bin/heroku open
13年1月10日木曜日
![Page 18: Railsの基礎](https://reader031.vdocuments.pub/reader031/viewer/2022020218/55660e3dd8b42aa6628b5389/html5/thumbnails/18.jpg)
つづく(かも)
13年1月10日木曜日