ruby on rails workshop

Post on 21-Jan-2018

66 Views

Category:

Software

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Ruby on RailsYos Riady yosriady.com

Today’s Agenda

● Intro to Ruby (40 minutes)● Rails (120 minutes)● QA (20 minutes)

Built On Rails

Ruby Language

codecademy.com/en/tracks/ruby

● Variables● Conditionals ● Loops + Iterators● Blocks● Data Structures● Classes

Installing Rails

https://lite.nitrous.io

Creating a New Rails Application

rails new myapp

cd myapp

bundle install

rails server

MVC Architecture

Building a Static Siterails generate controller Pages home

Now try and visit ‘/pages/home’.

What do you see?

Building a Static Site

Try adding a few more pages on your own.

Remember, for each page:● Add a new route in routes.rb● Add a new action in PagesController.rb● Add a view .erb file

Building a Static Site

Try adding the following in your action:def home

@now = Time.now

end

In your home.html.erb file, add: <%= @now %>

Building a Dynamic SiteAt the core, most database driven web sites are the same. They need to store records and provide a way to do the following:● Create new records in the database● Read or show the records in the database● Update existing records● Destroy or delete records

Because these 4 actions are so common, Rails includes the scaffold command to make creating them easier.

rails generate scaffold Confession title:string body:text

REST

Let’s add comments to our application!

Users can create Comments for a Confession, and have it visible under the Confession entry.

How do we do this?

Building a Dynamic Site

What do we need?● A new Comment model● A new controller for Comments

○ A ‘create’ action to save new Comments to the DB● A new route in routes.rb for

‘comments#create’● A view form for users to write a Comment

Building a Dynamic Site

Building a Dynamic Site

Hands-On

Q&AYos Riady yosriady.com

top related