1 of 107

Workshop 3 - Rails

2 of 107

13:35 - Check setup

13:50 - Start coding

15:00 - Break (15 min.)

16:30 - Break (15 min.)

17:30 - Review of task

18:00 - Wrap up

3 of 107

We’ll build a tiny tweet app.

4 of 107

It will be fun!

5 of 107

It will be the best tiny tweet app.

6 of 107

And it will look like cr*p!

7 of 107

Features:�

  • As a user I can view a list of tweets.
  • As a user I can view individual tweets.
  • As a user I can comment on tweets.�
  • Bonus features. (If we have time.)

8 of 107

Part 0: Setup

9 of 107

tinyurl.com/techladies

10 of 107

11 of 107

12 of 107

git init

git add -A

git commit -m "Initial commit"

git log

13 of 107

Part 1: Rails overview

14 of 107

15 of 107

16 of 107

Client

Server

HTTP

(Internet)

17 of 107

Front end

(HTML, JS, CSS)

Back end

(Ruby on Rails)

HTTP

(Internet)

18 of 107

Front end

(HTML, JS, CSS)

Back end

(Ruby on Rails)

HTTP

(Internet)

19 of 107

Frameworks let us

build stuff really

quickly.

20 of 107

Frameworks let us

build stuff really

quickly.

We just need

to make sure

we pick the

right one.

21 of 107

MVC

Model View Controller

22 of 107

Layered Architecture

23 of 107

Model

For describing objects, their relations, and storing data in the database.

24 of 107

View

For presenting data to the user. Also known as a user interface.

25 of 107

Controller

The glue that connects the view with the models.

26 of 107

Convention > Configuration

27 of 107

Recap:�

  • Rails is a framework written in Ruby.
  • It is based on the MVC pattern.
  • It is run on the server.
  • It sits in between the client (browser) and the database.
  • It uses Convention over Configuration.

28 of 107

Part 2: The database

29 of 107

Back end

(Ruby on Rails)

Database

30 of 107

31 of 107

32 of 107

33 of 107

Migrations

34 of 107

Recap:�

  • The database stores all user data.
  • It is described in the schema.
  • Changes to it are done in migrations.

35 of 107

Part 3: Resources & routing

36 of 107

37 of 107

Rails actions:

  • index
  • show
  • new + create
  • edit + update
  • destroy

38 of 107

Features:�

  • view a list of tweets. Tweet#index
  • view individual tweets. Tweet#show
  • comment on tweets. Comment#create

39 of 107

Routes

40 of 107

tinytweet.com

41 of 107

Features:�

  • view a list of tweets. Tweet#index
  • view individual tweets. Tweet#show
  • comment on tweets. Comment#create

42 of 107

tinytweet.com/tweets

Tweet#index

43 of 107

tinytweet.com/tweets/123

Tweet#show

44 of 107

tinytweet.com/tweets/123/comments

Comment#create

45 of 107

Recap:�

  • Resources are things in your database.
  • REST defines some actions for resources.
  • Each resource has its own routes.
  • Routes are stored in config/routes.rb
  • You can list routes with rake routes

46 of 107

Part 4: Tweets

47 of 107

Part 4a: Tweet Model

48 of 107

Model

For describing objects, their relations, and storing data in the database.

49 of 107

Tweet

Comment

50 of 107

rails generate model Tweet content:string

51 of 107

rake db:migrate

52 of 107

rails console

Tweet.all

CTRL + D

53 of 107

54 of 107

rails console

Tweet.create!(content: “...”)

CTRL + D

55 of 107

Recap:�

  • We have created a Tweet model.
  • We added the database table by migrating.
  • The model has a single attribute, content.
  • We have added a validation to ensure the content is between 1-20 characters.
  • We can use the console to test.

56 of 107

MVC

Model View Controller

57 of 107

Part 4b: Tweets index action

58 of 107

Controller

The glue that connects the view with the models.

59 of 107

rails generate controller Tweets

60 of 107

61 of 107

MVC

Model View Controller

62 of 107

Part 4c: Tweet index view

63 of 107

View

For presenting data to the user. Also known as a user interface.

64 of 107

app/views/tweets/index.html.erb

65 of 107

Convention > Configuration

66 of 107

67 of 107

MVC

Model View Controller

68 of 107

Part 4d: Tweet routes

69 of 107

70 of 107

71 of 107

Let's test it out!

72 of 107

Part 4e: Tweets show action

73 of 107

74 of 107

Part 4f: Tweets show view

75 of 107

app/views/tweets/show.html.erb

76 of 107

77 of 107

78 of 107

git add -A

git commit -m "Add Tweet resource"

git log

79 of 107

Let's test it out!

80 of 107

Part 5: Comments

81 of 107

Tweet

Comment

82 of 107

Part 5a: Comment Model

83 of 107

rails generate model Comment

content:string tweet:references

84 of 107

rake db:migrate

85 of 107

86 of 107

87 of 107

rails console

Tweet.first.comments

CTRL + D

88 of 107

Part 5b: Comment new form

89 of 107

90 of 107

91 of 107

Part 5d: Comment create route

92 of 107

93 of 107

94 of 107

Part 5d: Comment create action

95 of 107

96 of 107

97 of 107

98 of 107

99 of 107

Let's test it out!

100 of 107

git add -A

git commit -m "Add Comment resource"

git log

101 of 107

Part 6: Flash messages

102 of 107

103 of 107

104 of 107

Let's test it out!

105 of 107

Part 7: The task

106 of 107

https://github.com/TechLadies/technical-task

107 of 107

Thank you!