rails初心者レッスン lesson2 2edition

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

Upload: satomi-tsujita

Post on 27-May-2015

468 views

Category:

Technology


0 download

TRANSCRIPT

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

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

第2版Rails3.1

Lesson 2

2011年10月6日木曜日

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

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

2011年10月6日木曜日

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

Lesson1の復習

使ったコマンド $ rails new プロジェクト名 $ rails generate scaffold テーブル名 カラム:データ型 $ rake db:migrate $ rails console または rails c $ rails server または rails s $ rails -h $ rails generate -h $ rake -T $ rake routes

2011年10月6日木曜日

Page 4: Rails初心者レッスン lesson2 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の確認

$ rake routes

2011年10月6日木曜日

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

URLとリンク例:venue#indexからvenue#newへ$ rake routes で確認

new_venue GET /venues/new(.:format) {:action=>"new", :controller=>"venues"}

app/views/venues/index.html.erb の設定 <%= link_to 'New Venue', new_venue_path %>

一番前の部分を書く

URL

2011年10月6日木曜日

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

URLとリレーションURL設定とmodel設定は別のもの!!!

          app/models/以下に設定       config/routes.rbに設定たとえば、URL→http://host/(親)s/:(親)id/(子)s のとき、   config/routes.rbに以下の設定     resources :(親)s do     resources :(子)s     endmodel →app/models/(親).rb に以下の設定     has_many :(子)s    app/models/(子).rb に以下の設定     belongs_to :(親)

親 1 多 子

2011年10月6日木曜日

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

リレーションと設定例

URL設定 config/routes.rb     resources :venues do     resources :restaurants     end

→ http://host/venues/venue_id/restaurants

workshop

venue restaurant1多 多

1

多多neighbor中間テーブル

2011年10月6日木曜日

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

リレーションと設定例model設定 app/models/venue.rb     has_many :neighbors     has_many :restaurants, :through => :neighbors     app/models/restaurant.rb     has_many :neighbors     has_many :venues, :through => :neighbors     app/models/neighbors.rb     belongs_to :venue     belongs_to :restaurant     app/models/workshops.rbは考えてみましょう。^^

2011年10月6日木曜日

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

実装の順番    Create → Read → Update → Delete がいいらしい。 app/controllers/*_controller.rbの中では    new+create → index/show → edit → delete例:restaurant登録画面にvenueを表示する

viewでの呼び出し方(id引き継ぎ)

venueの詳細画面

restaurant追加ボタン

restaurant登録画面

URL:venues/:id URL:venues/:venue_id/restarurants/new

venue情報

restaurant登録

venue情報

同じもの2011年10月6日木曜日

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

viewでの呼び出し方(id引き継ぎ)

app/controller/restaurants_controller.rb

def new @restaurant = Restaurant.new @venue = Venue.find(params[:venue_id]) ・・・ end

def create @restaurant = Restaurant.new(params[:entry]) @restaurant.venue_id = params[:venue_id] ・・・ end

例:restaurant登録画面にvenueを表示する

app/views/restaurants/_form.html.erb

<%= form_for([@venue, @restaurant]) do |f| %> ・・・ <div class="field"> <%= f.label :venue_id %><br /> <%= @venue.name %><br /> <%= @venue.address %> </div> ・・・<% end %>

2011年10月6日木曜日

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

例:workshop登録画面にvenueを表示する

venueを表示するための情報を取ってこないといけない!

viewでの呼び出し方(セレクトボックス)

トップ画面

workshop登録ボタン

workshop登録画面

URL:welcome URL:workshops/new

勉強会情報

workshop登録

venue情報 ???

2011年10月6日木曜日

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

app/views/workshops/_form.html.erb

<%= form_for([@venues, @workshop]) do |f| %> ・・・ <div class="field"> <%= f.label :venue_id %><br /> <%= f.select :venue_id,

@venues.map{|m| [m.name, m.id]} %> </div> ・・・<% end %>

[label, value]

viewでの呼び出し方(セレクトボックス)

app/controller/workshos_controller.rb

def new @workshop = Workshop.new @venues = Venue.all ・・・ end

def create @workshop = Workshop.new(params[:entry]) @workshop.venue_id = params[:venue_id] ・・・ end

例:workshop登録画面にvenueを表示する

RubyのEnumerable#map

2011年10月6日木曜日

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

考えてみよう画面遷移を決めようURLを確認しようconfig/routes.rbを設定しようapp/model/*.rbを設定しようrake routesコマンドでURLを確認しながら、親子関係の親app/views/*/*.html.erbに子へのリンクを貼ってみよう子app/controllers/*_controller.rbで親情報を呼び出そう子app/views/*/*.html.erbで親情報を表示させよう

2011年10月6日木曜日

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

おまけview不要のテーブルを追加するコマンド$ rails generate model neighbor

venue_id:integer restaurant_id:integer (↑一行で書きます。)

$ rake db:migrate

controllerだけを作るコマンド$ rails generate controller neighbors

詳しくはLesson3で!s 要るよ!

2011年10月6日木曜日

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

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

2011年10月6日木曜日