15
Rails3 初心者レッスン by Minami.rb 第2版 Rails3.1 Lesson 1

Rails初心者レッスン lesson1 2edition

Embed Size (px)

Citation preview

Page 1: Rails初心者レッスン lesson1 2edition

Rails3 初心者レッスンby Minami.rb

第2版Rails3.1

Lesson 1

Page 2: Rails初心者レッスン lesson1 2edition

このレッスンでやること一覧1. Railsで何をする?どうしてRails?2. 作ってみよう事始め3. プロジェクトとテーブル4. Railsの役割分担5. Railsのディレクトリ構成6. scaffold(デフォルト)でのURL7. リレーションを考える8. 日本語対応9. 見た目をキレイに10.プラグインを使う11.ちょっとRubyに働いてもらう12.herokuに公開しよう

Page 3: Rails初心者レッスン lesson1 2edition

この初心者レッスンを全部聞くと、

簡単なものなら、自分で作れるようになるでしょう

Railsがわかった気になるでしょう

Minami.rb初心者レッスンの講師になれます

Page 4: Rails初心者レッスン lesson1 2edition

Rails で何をする?

例:ブログ、ホームページ、ECサイト、業務アプリ

有名なRails製サイト

twitter http://twitter.com/

Webアプリを作ります

クックパッド http://cookpad.com/

Page 5: Rails初心者レッスン lesson1 2edition

どうして Rails?

コマンドを実行するだけで、

データベースとの連携が意識せずできる

データの一覧、詳細がブラウザに表示できる

データの新規登録、編集などの画面・ボタンが用意される

=>簡単!

Page 6: Rails初心者レッスン lesson1 2edition

作ってみよう事始め

何作る?

テーブル構成は?

どんな画面遷移?

Page 7: Rails初心者レッスン lesson1 2edition

プロジェクトとテーブルプロジェクト作成

$ rails new プロジェクト名 e.g. rails new minamirb_blogテーブルの元ネタ作成

$ rails generate scaffold テーブル名 カラム:データ型 e.g. rails generate scaffold user name:string age:integer (データ型種類:string, integer, date, time, datetime, boolean)テーブル作成

$ rake db:migrate これがmodel(class)の名前で、かつ、controllerとviewは複数形(e.g.users_controller)になる

Page 8: Rails初心者レッスン lesson1 2edition

プロジェクトとテーブルテーブルを確認する(RailsのDBを含めたirbが起動する)

$ rails console または rails c >> User #最初は大文字Railsを起動する

$ rails server または rails s →ブラウザで http://localhost:3000 にアクセス!

コマンドのヘルプいろいろ $ rails -h $ rails generate -h $ rails generate scaffold -h $ rake -T

Page 9: Rails初心者レッスン lesson1 2edition

JavaScript/CoffeeScriptCSS/SCSS など

Railsの役割分担

controllers

views

DB

Ruby

modelsRuby

webサーバー(apacheとか)

ブラウザ

rack

sprockets

HTML.erb

Ruby

Ruby

SQL

Page 10: Rails初心者レッスン lesson1 2edition

Railsのディレクトリ構成prj.

images

public

app

config

db

lib

log

script

test

tmp

vendor

*.gif, *.png

stylesheets

javascripts

application.css

models

controllers

models

views

assets

app

Page 11: Rails初心者レッスン lesson1 2edition

Railsのディレクトリ構成prj.

・・・migratedb

app

config

lib

log

public

script

test

tmp

vendor

db

Page 12: Rails初心者レッスン lesson1 2edition

Railsのディレクトリ構成prj.

・・・routes.rb

config

app

db

lib

log

public

script

test

tmp

vendor

config

Page 13: Rails初心者レッスン lesson1 2edition

scaffold(デフォルト)でのURLURLの基本構成

http://host/resources #action => index http://host/resources/new #action => new http://host/resources #action => create http://host/resources/id #action => show http://host/resources/id/edit #action => edit http://host/resources/id #action => update http://host/resources/id #action => destroyURL(routes)の確認

$ rake routesLesson2で設定とかやります

Page 14: Rails初心者レッスン lesson1 2edition

app/controllers/users_controller.rbclass UsersController < ApplicationController # GET /users # GET /users.json def index @users = User.all

respond_to do |format| format.html # index.html.erb format.json { render json: @users } end end

app/views/index.html.erb<h1>Listing users</h1>・・・<% @users.each do |user| %> <tr> <td><%= user.name %></td> <td><%= user.age %></td> <td><%= link_to 'Show', user %></td> <td><%= link_to 'Edit', edit_user_path(user) %></td> <td><%= link_to 'Destroy', user, confirm: 'Are you sure?', method: :delete %></td> </tr><% end %>

Page 15: Rails初心者レッスン lesson1 2edition

Lesson1は、これで終わりです。お疲れさまでした。