Rails for Zombies Answers
If you were looking for the Rails for Zombies answers, welcome to this collection.
Rails for Zombies is one of the best online courses for beginners. It consists of 5 levels, each ending with a Lab – a set of 5-6 practice exercises. You can’t open the next level before you complete all exercises of your current level. Here is the course structure:
Level 1: Deep in the CRUD (Practice: Zombie Lab 1)
- Find 1
- Create
- Find 2
- Query
- Update
- Destroy
Level 2: Models (Practice: Zombie Lab 2)
- Models
- Validations 1
- Validations 2
- Validations 3
- Belongs To
- Relationship Find
Level 3: View – Visual Representation (Practice: Zombie Lab 3)
- Views
- Linking (ERB)
- Each Blocks
- If
- Linking in Blocks
Level 4: Controllers – Brains of the App (Practice: Zombie Lab 4)
- Show Action
- Respond To
- Controller Create Action
- Controller Before Action
Level 5: Routing Through Rails (Practice: Zombie Lab 5)
- Rsource Route
- Route Matching
- Route Redirecting
- Root Route
- Named Route
Lab 1
Challenge 1 FIND 1
Find a Zombie where the ID is 1:
Zombie.find(1)
#
Challenge 2 CREATE
Create a new Zombie:
Zombie.create
# creates an empty zombie line in the Zombies table
#
Challenge 3 FIND 2
Find the last Zombie in the database, but don’t use IDs:
Zombie.last
# returns the last zombie in the Zombies table
Challenge 4 QUERY
Find all Zombies ordered by their names:
Zombie.order(:name)
# returns all zombies ordered by name
Challenge 5 UPDATE
Update Zombie 3’s graveyard to ‘Benny Hills Memorial’:
z = Zombie.find(3)
#
z.graveyard = "Benny Hills Memorial"
z.save
Challenge 6 DESTROY
Destroy the Zombie with an ID of 3:
z = Zombie.find(3)
#
z.destroy
# or Zombie.find(3).destroy
Lab 2
Challenge 1 CREATE MODEL
Define a Zombie model:
app/models/zombie.rb
class Zombie < ActiveRecord::Base
has_many :tweets
end
Challenge 2 VALIDATIONS 1
Add a validation that checks for the presence of a Zombie's name:
app/models/zombie.rb
class Zombie < ActiveRecord::Base
validates_presence_of :name
end
Challenge 3 VALIDATIONS 2
Add a validation that checks for the uniqueness of a Zombie's name:
app/models/zombie.rb
class Zombie < ActiveRecord::Base
validates_uniqueness_of :name
end
Challenge 4 VALIDATIONS 3
Validate both the uniqueness and the presence of a Zombie's name on a single line, using the new validation syntax:
app/models/zombie.rb
class Zombie < ActiveRecord::Base
validates :name, presence: true, uniqueness: true
end
Challenge 5 BELONGS TO
A Weapon belongs to a Zombie. Create that relationship:
app/models/zombie.rb
class Weapon < ActiveRecord::Base
belongs_to :zombie
end
Challenge 6 RELATIONSHIP FIND
Assuming the models and relationships are properly defined, find all the weapons that belong to Zombie 'Ashley':
Console
Zombie.find(1).weapons
# ]>
# found all of Ashley's weapons.
Lab 3
Challenge 1 VIEWS
Print out the zombie's name and graveyard:
app/views/zombies/show.html.erb
<% zombie = Zombie.first %>
<%= zombie.name %>
<%= zombie.graveyard %>
Challenge 2 LINKING
Link to the zombie's show page. Use the zombie's name as the anchor text:
app/views/zombies/show.html.erb
<% zombie = Zombie.first %>
<%= link_to zombie.name, zombie %>
Challenge 3 EACH BLOCKS
Use an each do block to print the names of all the zombies:
app/views/zombies/index.html.erb
<% zombies = Zombie.all %>
-
<% zombies.each do |z| %>
<%= z.name %>
<% end %>
Challenge 4 IF STATEMENT
In the each block, if a zombie has more than one tweet, print out SMART ZOMBIE:
app/views/zombies/index.html.erb
<% zombies = Zombie.all %>
-
<% zombies.each do |zombie| %>
- <%= zombie.name %> <% if zombie.tweets.size > 1 %> SMART ZOMBIE <% end %> <% end %>
Challenge 5 LINKING IN BLOCKS
In the each block, make the zombie's name link to its edit page:
app/views/zombies/index.html.erb
<% zombies = Zombie.all %>
-
<% zombies.each do |zombie| %>
- <%= link_to zombie.name, edit_zombie_path(zombie) %> <% end %>
Lab 4
Challenge 1 SHOW ACTION
Create the show action for the ZombiesController which finds a Zombie based on params[:id]. Store the Zombie object to an instance variable named @zombie:
app/controllers/zombies_controller.rb
class ZombiesController < ApplicationController
def show
@zombie = Zombie.find(params[:id])
end
end
Challenge 2 RESPOND TO
Finish the respond_to block so the action returns the XML of the @zombie record:
app/controllers/zombies_controller.rb
class ZombiesController < ApplicationController
def show
@zombie = Zombie.find(params[:id])
respond_to do |format|
format.xml { render xml: @zombie }
end
end
end
Challenge 3 CONTROLLER CREATE ACTION
Write a create action that will create a new Zombie from the params and then redirect to the created Zombie's show page. Make sure to use Rails 4 strong_parameters:
app/controllers/zombies_controller.rb
# params = {zombie: {name: 'Gregg', graveyard: 'TBA'}}
class ZombiesController < ApplicationController
def create
@zombie = Zombie.create(zombie_params)
redirect_to zombie_path(@zombie)
end
private
def zombie_params
params.require(:zombie).permit(:name, :graveyard)
end
end
Challenge 4 CONTROLLER BEFORE ACTION
Add a before_action that calls a method to check if a Zombie has tweets. Redirect to zombies_path if the zombie doesn't have tweets, only on show:
app/controllers/zombies_controller.rb
# params = {zombie: {name: 'Gregg', graveyard: 'TBA'}}
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
Lab 5
Challenge 1 RESOURCE ROUTE
Create a resources route for zombies:
config/routes.rb
TwitterForZombies::Application.routes.draw do
resources :zombies
end
Challenge 2 ROUTE MATCHING
Create a custom route so that '/undead' will go to the undead action on the ZombiesController:
config/routes.rb
TwitterForZombies::Application.routes.draw do
get 'undead' => 'zombies#undead'
end
Challenge 3 ROUTE REDIRECTING
Create a redirect for '/undead' to '/zombies':
config/routes.rb
TwitterForZombies::Application.routes.draw do
get '/undead', to: redirect('/zombies')
end
Challenge 4 ROOT ROUTE
Create a root route to the ZombiesController index action:
config/routes.rb
TwitterForZombies::Application.routes.draw do
root to: 'zombies#index'
end
Challenge 5 NAMED ROUTE
Create a named route. It should generate a path like '/zombies/:name' where :name is a parameter, and points to the index action in ZombiesController. Name the route 'graveyard':
config/routes.rb
TwitterForZombies::Application.routes.draw do
get '/zombies/:name', to: 'zombies#index', :as => 'graveyard'
end
[/vc_column_text][/vc_column][/vc_row]