You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: sites/en/intro-to-rails/creating_a_migration.step
+11-43Lines changed: 11 additions & 43 deletions
Original file line number
Diff line number
Diff line change
@@ -4,30 +4,24 @@ goals {
4
4
5
5
message "The suggestotron has a list of topics that people can vote on. We'll store our topics in the database. In this step you'll do the following:"
6
6
7
-
goal "Create a database table for topics with a title and a description"
7
+
goal "Create a a simple *Table* in the database for topics with a title and a description"
8
+
goal "Automatically generate the corresponding *Scaffold* in Rails (namely, the *Model*, the *View*, and the *Controller*)."
8
9
9
-
message "In this step we'll learn a bit about MVC (Model-View-Controller) architecture. By the end of this step you should understand the following concepts:"
* `generate scaffold` tells rails to create everything necessary to get up and running with topics.
26
18
* `topic` tells rails the name of the new model.
27
-
* `title:string` says that topics have a title, which is a string.
28
-
* `description:text` says that topics have a description which is a "text". (We're befuddled by the difference too.)
19
+
* `title:string` says that topics have a title, which is a "string".
20
+
* `description:text` says that topics have a description which is a "text". (What's the difference between "string" and "text"? Basically "text" is for strings that might be very long.)
21
+
29
22
MARKDOWN
30
-
message "If you want, take some time to poke around the files listed in this step."
23
+
message "If you want, take some time to poke around the files listed in this step. You can learn about them in the [Rails Architecture](rails_architecture) page."
24
+
link "rails_architecture"
31
25
}
32
26
33
27
step {
@@ -40,40 +34,14 @@ explanation {
40
34
41
35
h2 "Rake"
42
36
message <<-MARKDOWN
43
-
`rake` _(Ruby Make)_ is a tool that allows you to run small Ruby programs (**tasks**) that you use often in your application. Here, `rake db:migrate` is a task provided by the Rails framework.
44
-
45
-
You can run `rake -T` to see a list of all the `rake` commands your app currently responds to, along with a short description of each task.
46
-
MARKDOWN
47
-
48
-
h2 "Explaining MVC and Records"
49
-
50
-
img src: "img/mvc.png", alt: "MVC"
51
-
52
-
message "Rails implements a very specific notion of the **Model/View/Controller** pattern, which guides how you structure your web applications."
53
-
54
-
h3 "Model"
55
-
message <<-MARKDOWN
56
-
* saves data to the database
57
-
* accesses data from the database
58
-
* bridge between the database and objects
59
-
MARKDOWN
37
+
`rake` _(ruby make)_ is a tool that allows you to run small Ruby programs (**tasks**) that you use often in your application.
60
38
61
-
h3 "View"
62
-
message <<-MARKDOWN
63
-
* display the data for human (or machine) consumption
64
-
* webpages are views
65
-
MARKDOWN
39
+
Here, `rake db:migrate` is a task provided by the Rails framework. It creates a bunch of new files, including a *migration*, a *model*, a *view*, and a *controller*.
66
40
67
-
h3 "Controller"
68
-
message <<-MARKDOWN
69
-
* acts as the glue between the models and the views
70
-
* combines data from multiple models
71
-
* summarizes and filters data
72
41
MARKDOWN
73
42
74
-
message <<-MARKDOWN
75
-
In MVC, models, views, and controllers have very specific jobs. Separating responsibilities like this make it easy to maintain and extend rails applications. When responsibilities become muddied it gets much harder to debug issues and add new functionality.
76
-
MARKDOWN
43
+
tip "You can run `rake -T` to see a list of all the `rake` commands your app currently responds to, along with a short description of each task."
goal "Create a database table for topics with a title and a description"
4
+
5
+
message "In this step we'll learn a bit about Rails architecture. By the end of this step you should understand the following concepts:"
6
+
7
+
ul {
8
+
li "Table"
9
+
li "Model"
10
+
li "View"
11
+
li "Controller"
12
+
}
13
+
}
14
+
15
+
explanation {
16
+
17
+
h2 "Rails architecture and its relation to the database"
18
+
19
+
img src: "img/mvc.png", alt: "MVC"
20
+
21
+
message "Rails implements a very specific notion of the **Model/View/Controller** pattern, which guides how you structure your web applications."
22
+
23
+
h3 "Model"
24
+
message <<-MARKDOWN
25
+
* For all the Models we create in RailsBridge, Model objects have a corresponding record in the the database. The name of the table in the database is the plural version of the Model's class name. For example, if the Model is called 'Duck', it will automatically query or write to the 'ducks' table in the database.
26
+
* Methods internal to Rails make it easy to automatically write records to the the database and query the database to get the records again later.
27
+
* The Model is a bridge between the database and your application's code.
28
+
MARKDOWN
29
+
30
+
h3 "View"
31
+
message <<-MARKDOWN
32
+
* The View generates the HTML that will be displayed in the browser.
33
+
* View files are written in ERB, a templating language. It contains HTML with Ruby code embedded in it. The ruby variables in the view stand as placeholders for content that will be filled in when a user requests the page.
34
+
* (There are several other templating languages available, but in RailsBridge we always stick to ERB.)
35
+
MARKDOWN
36
+
37
+
h3 "Controller"
38
+
message <<-MARKDOWN
39
+
* Controllers pass Ruby objects between the Models and the Views.
40
+
* Each url corresponds to a specific method in a Controller.
41
+
* After this step, when you visit any page in your application, that request will be handled by a method in a Controller.
42
+
MARKDOWN
43
+
44
+
message <<-MARKDOWN
45
+
When Models, Views and Controllers are all put together, they follow a pattern: Given a URL, Rails will look up which Controller method (also called an "Action") to use. The Controller Action will use methods in a corresponding Model. The Model will need to read or write to the database, and return an object containing that data to the Controller. The Controller will take that object and pass it to the View. (Actions normally have a corresponding View file, and Rails will automatically find and use that file.)
46
+
47
+
Models, Views and Controllers each have specific jobs. Separating responsibilities like this make it easier to develop, especially as it gets bigger. (When each file has a clear responsibility it's easier to fix problems and add new features.)
0 commit comments