Unidad VI Vamos a Aprender con Rails For Zombies


RAILS FOR ZOMBIES

Es una herramienta de codeSchool, que permite aprender y poner en practica la sintaxis y los conceptos básicos que utiliza rails para desarrollar aplicaciones. Consta de 5 niveles donde se podrán generar CRUD, MODELS, CONTROLLERS, VIEWS Y ROUTES.

Level 1: Deep in the Crud Find I
Try to find a Zombie where the ID is 1.
$ t=Zombie.find(1)
Create
Create a new Zombie.
$t=Zombie.create(id:4,name:"Judith",graveyard:"My syster")
Find II
Find the last Zombie in the database, but don't use IDs
$ Zombie.last 
Query
Find all Zombies ordered by their names.
$ Zombie.order(:name) 
Update
Update Zombie 3's graveyard to 'Benny Hills Memorial'
$ t = Zombie.find(3)
$ t.update(graveyard:'Benny Hills Memorial')
Destroy
Destroy the Zombie with an ID of 3.
$ t = Zombie.find(3)
$ t.destroy
Level 2: Models Taste Like Chicken 

Create Model
Define a Zombie model. 
class Zombie ActiveRecord::Base
end
Validations I 
Add a validation that checks for the presence of a Zombie's name 

class Zombie < ActiveRecord::Base
     validates_presence_of:name
end
Validations II 
Add a validation that checks for the uniqueness of a Zombie's name.
class Zombie < ActiveRecord::Base
     validates_uniqueness_of:name
end
Validations III 
Validate both the uniqueness and the presence of a Zombie's name on a single line, using the new validation syntax.
class Zombie ActiveRecord::Base
     validates:name, uniqueness:true, presence:true
end
Belongs To 
 A Weapon belongs to a Zombie. Create that relationship.
class Weapon ActiveRecord::Base
     belongs_to :zombie
end
class Zombie ActiveRecord::Base
     has_many :weapons
end
class Weapon ActiveRecord::Base
     belongs_to :zombie
end
Relationship Find
Assuming the models and relationships are properly defined, find all the weapons that belong to Zombie 'Ashley'.
class Zombie ActiveRecord::Base
     has_many :weapons
end
class Weapon ActiveRecord::Base
belongs_to :zombie
end
$ ashley = Zombie.find(1) 

Level 3: The Views Ain't Always Pretty
VIEWS SIMPLE 
Print out the zombie's name and graveyard.
<% zombie = Zombie.first %>
<%= zombie.name %>
<%= zombie.graveyard %>
LINKING
Link to the zombie's show page. Use the zombie's name as the anchor text 
<% zombie = Zombie.first %> <%= link_to zombie.name, zombie %>
 
EACH BLOCKS
Use an each block to print the names of all the zombies. 
<% zombies = Zombie.all %> 
<% Loop through each zombies %>
     <%= zombies.name %>
<% end %> 
<% zombies.all.each do |zombie| %>
     <%= zombie.name %>
<% end %>
IF
In the each block, if a zombie has more than one tweet, print out SMART ZOMBIE. 
<% zombies = Zombie.all %> 
<% zombies.each do |zombie| %>
     <%= zombie.name %>
     <% if zombie.tweets.size > 1 %>
           SMART ZOMBIE
     <% end %> 
<% end %>
LINKING IN BLOCKS
In the each block, make the zombie's name link to its edit page. 
<% zombies = Zombie.all %> 
<% zombies.each do |zombie| %>
     <%= zombie.name %>
     <%= link_to zombie.name, edit_zombie_path(zombie) %> 
<% end %>
 
Level 4: Controllers Must Be Eaten
 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.
class ZombiesController < ApplicationController 
     def show 
         @zombie = Zombie.find(params[:id]) 
     end 
end
RESPOND TO
Finish the respond_to block so the action returns the XML of the @zombie record 
class ZombiesController < ApplicationController 
     def show
         @zombie = Zombie.find(params[:id])
         respond_to do |format| format.xml {
            render xml: @zombie } 
         end
     end 
end
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. 
class ZombiesController < ApplicationController 
     def create 
         @zombie = Zombie.create(zombie_params)
         redirect_to @zombie 
     end 
     private 
     def zombie_params
         params.require(:zombie).permit(:name, :graveyard) 
  end 
end
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. 
class ZombiesController < ApplicationController 
     before_action :find_zombie, only: :show 
     def show
          render action: :show 
     end 
     def find_zombie 
         @zombie = Zombie.find params[:id] 
         if @zombie.tweets.size == 0 
               redirect_to zombies_path 
         end 
     end 
end
Level 5: Routing Into Darkness
Resource Route
Create a resources route for zombies. 
TwitterForZombies::Application.routes.draw do
     resources :zombies get '/new_zombie' => 'zombies#new' 
end
Route Matching
Create a custom route so that '/undead' will go to the undead action on the ZombiesController.
TwitterForZombies::Application.routes.draw do resources :zombies 
      get '/undead' => 'zombies#undead' 
end
Route Redirecting
TwitterForZombies::Application.routes.draw do get '/undead' => redirect('/zombies') end Root Route Create a root route to the ZombiesController index action. 
TwitterForZombies::Application.routes.draw 
     do root to: "zombies#index" 
end
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'
TwitterForZombies::Application.routes.draw do 
     get '/zombies/:name', to: 'zombies#index', as: 'graveyard' 
end 

Me pareció súper genial esta modalidad ya que puse en practica los conocimientos adquiridos en los videos del curso... Y ya que se realizan las practicas en vivo se puede saber si tienes algún error en y se puede corregir al momento.

Comentarios

Entradas más populares de este blog

Unidad IV Película Motivadora Talentos Ocultos

Unidad III "Grace Murray Hopper"

Unidad VII Sheryl Kara Sandberg