2011年9月3日土曜日

Rails for Zombies 解答

Rails for Zombiesについて書いた前記事のつづき!

解答のまとめ。
Rails初心者の私がやったものだから、もしかしたら違うところがあるかも。
でも一応このサイトでは、正解!って表示されたから、多分あってるはず。多分…

【解答】
Level1 Challenge
1.Find
z = Zombie.find(1)
p z
変数zにZombieのなかからidが1のものを探してという命令を入れる。
そのあと、pでzを出力。
Zombiesにしたくなるところですが、モデルのクラスは単数系じゃなきゃいけないらしい。


2.Create
z = Zombie.new
p z
z.save 
newで新しいZombieをつくって、 変数zの中に入れる。
pでzを出力。その後、z.saveでセーブ。


3.find 2
z = Zombie.last
z.save
p z
最後のZombie探すときは、「変数=クラス名.last」。
saveの位置変えないと通らなかったー。
なんでだろう。


4.Query 1
z = Zombie.all
p z
要素を全部取り出す時はallを使うらしい。
なんでsaveいらないんだろ。こんど講師の人に聞いてみよう。


5.Update
z = Zombie.find(3)
z.update_attributes(:graveyard => "Benny Hills Memorial")

6.Destroy
Zombie.find(3).destroy 

Level2 Challenge
1.Create Model
class Zombie < ActiveRecord::Base
end
Zombieって書いたクラスからzombiesのテーブルに接続するときの架け橋となるのが 「ActiveRecord::Base」…なのではないか。と予測。


2.Validates 1
class Zombie < ActiveRecord::Base
validates_presence_of :name
end
「validates_presence_of」は未入力の欄があったらfalseを返してくれるらしい。


3.Validates 2
class Zombie < ActiveRecord::Base
validates_uniqueness_of :name
end
これは重複があったらfalseを返してくれるみたい。


4.Validates 3
class Zombie < ActiveRecord::Base
validates :name, :uniqueness => true, :presence => true
end

5.Belongs to
class Weapon < ActiveRecord::Base
belongs_to :zombie
end 
zombieは単数系。

6.Relationship Find
class Weapon < ActiveRecord::Base
belongs_to :zombie
ash = Zombie.find(1)
ash.weapons
end


Level3 Challenge
1.Views simple
<% zombie = Zombie.first %>
<h1><%= zombie.name%></h1>
<p>
<%= zombie.graveyard %>
</p>
HTMLきた!だいすき!


2.Linking
<% zombie = Zombie.first %>
<p>
<%=link_to zombie.name, zombie %>
</p>
ここちょっと難しかった。
リンクテキストをzombieのお名前(zombie.name)にして、
後ろにzombiesのページを。


3.Each Blocks
<% zombies = Zombie.all %>
<ul>
<% zombies.each do |zombie| %>
<li>
<%= zombie.name %>
</li>
<% end %>
</ul>
<% # insert block here %>以外のところにもコード書く。ややこしいけど。


4.If
<% zombies = Zombie.all %>
<ul>
<% zombies.each do |zombie| %>
<li>
<%= zombie.name %>
<% if zombies.size > 1 %>
<em>SMART ZOMBIE</em>
<% end %>
</li>
<% end %>
</ul>

5.Linking in Blocks
<% zombies = Zombie.all %>
<ul>
<% zombies.each do |zombie| %>
<li>
<%= link_to zombie.name, edit_zombie_path(zombie) %>
</li>
<% end %>
</ul>
問題文あんま読んでなくて(英語だし)苦労した!
よく読むと簡単だった。



Level4 Challenge
1.Show Action

def show
@zombie = Zombie.find(params[:id])
end

2.Respond To 
def show
@zombie = Zombie.find(params[:id])
respond_to do |format|
format.xml {render :xml => @zombie}
end
end

 3.Controller Create Action
def create
@zombie = Zombie.create(params[:zombie])
redirect_to (@zombie)
end

4.Controller Before Filter 
class ZombiesController < ApplicationController

before_filter :find_zombie
before_filter :has_tweet, :only => [:show]

def has_tweet
if @zombie.tweets.count < 1
redirect_to zombies_path
end
end

def show
render :action => :show
end

def find_zombie
@zombie = Zombie.find params[:id]
end

end
難しい。新しいメソッド(def)作るのか…



Level5 Challenge
1.Resource Route
RailsForZombies::Application.routes.draw do
resources :zombies
end
ここまでは何とか!


2.Route Matching
RailsForZombies::Application.routes.draw do
resources :zombies
match 'undead' => "Zombies#undead"
end


3.Route Redirecting
RailsForZombies::Application.routes.draw do
match 'undead' => redirect('/zombies')
end


4.Root Route
RailsForZombies::Application.routes.draw do
root :to => "Zombies#index"
end 


5.Named Route 
RailsForZombies::Application.routes.draw do
match '/zombies/:name' => 'Zombies#index', :as =>'graveyard'
end
最初match ':name' => 'Zombies#index', :as =>'graveyard'って書いてた。
問題文はよく読もう。



というわけで全部クリアしましたー!!!!


わーい!わーい!

プログラミングできる人だと数時間で終わるらしいですね。
10数時間かかった気がする。

1 件のコメント:

  1. 参考になりました♪
    ありがとうございます(*´∀`)

    返信削除