My Rails for Zombies Answers (with extra credit)

Level 1

Challenge 1

Zombie.find(1)

extra challenge

Zombie.all

Challenge 2

Zombie.create(name: "Nick", graveyard: "Adelaide")

extra challenge

Zombie.find(4)

Challenge 3

Zombie.last

extra challenge

Zombie.first

Challenge 4

Zombie.order(:name)

extra challenge 

Zombie.limit(2)

Challenge 5

Zombie.find(3).update_attributes(graveyard: "Benny Hills Memorial")

extra challenge

Zombie.find(3).update_attributes(graveyard: "Herp", name: "Derp")

Challenge 6

Zombie.destroy(3)

extra challenge 

Zombie.destroy_all

Level 2

Challenge 1

class Zombie < ActiveRecord::Base
end

extra challenge

Zombie.count

Challenge 2

class Zombie < ActiveRecord::Base
    validates :name, presence: true
end

extra challenge

zomb = Zombie.new(graveyard: "Herp")
zomb.save

Challenge 3

class Zombie < ActiveRecord::Base
    validates :name, uniqueness: true
end

extra challenge

zomb = Zombie.new(name: "Ash", graveyard: "Herp")
zomb.save

Challenge 4

class Zombie < ActiveRecord::Base
    validates :name, presence: true, uniqueness: true
end

Challenge 5

class Weapon < ActiveRecord::Base
    belongs_to :zombie
end

extra challenge

Weapon.create(name: "Drop Saw", strength: 9001, zombie_id: 1)

Challenge 6

Zombie.find(1).weapons

extra challenge

Zombie.find(1).weapons.count

Level 3

Challenge 1

<%= zombie.name %>

<%= zombie.graveyard %>

Challenge 2

<%= link_to zombie.name, zombie %>

Challenge 3

    <% zombies.each do |z| %> <%= z.name %> <% end %>

Challenge 4

    <% zombies.each do |zombie| %>

  • <%= zombie.name %> <% if zombie.tweets.size > 1 %> SMART ZOMBIE <% end %>
  • <% end %>

Challenge 5

    <% zombies.each do |zombie| %>

  • <%= link_to zombie.name, edit_zombie_path(zombie)%>
  • <% end %>

Level 4

Challenge 1

class ZombiesController &lt; ApplicationController
  def show
    @zombie = Zombie.find(params[:id])
  end
end

Challenge 2

class ZombiesController &lt; ApplicationController
  def show
    @zombie = Zombie.find(params[:id])

    respond_to do |format|
      format.xml { render xml: @zombie }
    end
  end
end

Challenge 3

class ZombiesController &lt; ApplicationController
  def create
    @zombie = Zombie.create(params[:zombie])
    redirect_to @zombie
  end
end

Challenge 4

class ZombiesController &lt; ApplicationController
  before_filter :find_zombie

  def show
    render :action =&gt; :show
  end

  def find_zombie
    @zombie = Zombie.find params[:id]
    if @zombie.tweets.size &lt; 1 
      redirect_to @zombie
    end
  end
end

Level 5

Challenge 1

TwitterForZombies::Application.routes.draw do
  resources :zombies
end

Challenge 2

TwitterForZombies::Application.routes.draw do
  match '/undead' =&gt; "zombies#undead"
end

Challenge 3

TwitterForZombies::Application.routes.draw do
  match '/undead' =&gt; redirect('/zombies')
end

Challenge 4

TwitterForZombies::Application.routes.draw do
  match '/' =&gt; 'zombies#index'
end

Challenge 5

TwitterForZombies::Application.routes.draw do
  match '/zombies/:name' =&gt; 'zombies#index', :as =&gt; 'graveyard'
end

About Nick

Melbourne based web developer that loves building front-end systems with anything from vanilla JS to React. Infosec student and active learner. Subscribe to the blog

www ·

4 thoughts on “My Rails for Zombies Answers (with extra credit)”

  1. Vishal

    Thanks, I got stucked at one point and you saved my lot of time.
    Thanks again.

  2. Shady

    Thank you very much! This is very helpful in the learning process

  3. Electric_castle

    LEVEL 4 Challenge 4

    class ZombiesController < ApplicationController
    before_action :find_zombie
    before_action :check_tweets, only: :show

    def show
    render action: :show
    end

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

    def check_tweets
    if @zombie.tweets.size == 0
    redirect_to zombies_path
    end
    end

    end

  4. sads

    thank you,
    so much

Leave a Reply

Your email address will not be published.