kitabu 1.0.6 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +5 -3
- data/.travis.yml +18 -0
- data/CHANGELOG.md +9 -0
- data/Gemfile.lock +67 -50
- data/README.md +235 -0
- data/attachments/browser-version.png +0 -0
- data/attachments/cover.png +0 -0
- data/attachments/kitabu.epub +0 -0
- data/attachments/kitabu.mobi +0 -0
- data/attachments/kitabu.pdf +0 -0
- data/{spec/support/mybook/output → examples/kitabu/output/epub/images}/.gitkeep +0 -0
- data/examples/kitabu/output/epub/images/kitabu-icon.png +0 -0
- data/examples/kitabu/output/epub/images/kitabu-icon.svg +19 -0
- data/examples/kitabu/output/epub/images/kitabu-word.png +0 -0
- data/examples/kitabu/output/epub/images/kitabu-word.svg +14 -0
- data/examples/kitabu/output/epub/images/kitabu.png +0 -0
- data/examples/kitabu/output/epub/images/kitabu.svg +20 -0
- data/examples/kitabu/output/epub/section_0.html +266 -0
- data/examples/kitabu/output/epub/section_1.html +246 -0
- data/examples/kitabu/output/epub/section_2.html +520 -0
- data/examples/kitabu/output/epub/section_3.html +282 -0
- data/examples/kitabu/output/epub/section_4.html +276 -0
- data/examples/kitabu/output/epub/styles/epub.css +437 -0
- data/examples/kitabu/output/epub/styles/html.css +712 -0
- data/examples/kitabu/output/epub/styles/pdf.css +840 -0
- data/examples/kitabu/output/epub/styles/print.css +1278 -0
- data/examples/kitabu/output/epub/toc.html +37 -0
- data/{spec/support/mybook/templates/epub/style.css → examples/kitabu/output/images/.gitkeep} +0 -0
- data/examples/kitabu/output/images/kitabu-icon.png +0 -0
- data/examples/kitabu/output/images/kitabu-icon.svg +19 -0
- data/examples/kitabu/output/images/kitabu-word.png +0 -0
- data/examples/kitabu/output/images/kitabu-word.svg +14 -0
- data/examples/kitabu/output/images/kitabu.png +0 -0
- data/examples/kitabu/output/images/kitabu.svg +20 -0
- data/examples/kitabu/output/kitabu.epub +0 -0
- data/examples/kitabu/output/kitabu.html +513 -0
- data/examples/kitabu/output/kitabu.mobi +0 -0
- data/examples/kitabu/output/kitabu.pdf +0 -0
- data/examples/kitabu/output/kitabu.pdf.html +729 -0
- data/examples/kitabu/output/kitabu.print.html +729 -0
- data/examples/kitabu/output/kitabu.print.pdf +0 -0
- data/examples/kitabu/output/kitabu.txt +440 -0
- data/examples/kitabu/output/styles/epub.css +437 -0
- data/examples/kitabu/output/styles/html.css +712 -0
- data/examples/kitabu/output/styles/pdf.css +840 -0
- data/examples/kitabu/output/styles/print.css +1278 -0
- data/kitabu.gemspec +7 -5
- data/lib/kitabu.rb +10 -20
- data/lib/kitabu/cli.rb +0 -5
- data/lib/kitabu/dependency.rb +0 -4
- data/lib/kitabu/exporter.rb +2 -0
- data/lib/kitabu/extensions/rouge.rb +9 -0
- data/lib/kitabu/generator.rb +9 -21
- data/lib/kitabu/helpers.rb +47 -0
- data/lib/kitabu/markdown.rb +31 -0
- data/lib/kitabu/parser.rb +21 -3
- data/lib/kitabu/parser/epub.rb +31 -18
- data/lib/kitabu/parser/html.rb +48 -29
- data/lib/kitabu/parser/mobi.rb +1 -1
- data/lib/kitabu/parser/pdf.rb +52 -8
- data/lib/kitabu/version.rb +2 -2
- data/spec/kitabu/cli/export_spec.rb +4 -4
- data/spec/kitabu/cli/new_spec.rb +2 -2
- data/spec/kitabu/markdown_spec.rb +24 -0
- data/spec/kitabu/parser/html_spec.rb +20 -25
- data/spec/kitabu/parser/mobi_spec.rb +14 -0
- data/spec/kitabu/parser/pdf_spec.rb +18 -1
- data/spec/kitabu/parser/txt_spec.rb +14 -0
- data/spec/spec_helper.rb +10 -6
- data/spec/support/mybook/config/helper.rb +4 -29
- data/spec/support/mybook/config/kitabu.yml +0 -10
- data/spec/support/mybook/templates/epub/cover.erb +4 -3
- data/{templates → spec/support/mybook/templates/epub}/cover.png +0 -0
- data/spec/support/mybook/templates/epub/page.erb +3 -2
- data/spec/support/mybook/templates/html/layout.erb +10 -13
- data/spec/support/mybook/templates/styles/epub.scss +3 -0
- data/spec/support/mybook/templates/styles/html.scss +3 -0
- data/spec/support/mybook/templates/styles/pdf.scss +3 -0
- data/spec/support/mybook/templates/styles/print.scss +3 -0
- data/spec/support/mybook/text/{01_Markdown_Chapter.markdown → 01_Markdown_Chapter.md} +2 -3
- data/spec/support/mybook/text/02_ERB_Chapter.md.erb +7 -0
- data/spec/support/mybook/text/{04_With_Directory/Some_Chapter.mkdn → 03_With_Directory/Some_Chapter.md} +0 -0
- data/spec/support/mybook/text/{CHANGELOG.textile → CHANGELOG.md} +2 -2
- data/spec/support/mybook/text/{TOC.textile → TOC.md} +0 -0
- data/spec/support/mybook/text/{_00_Introduction.markdown → _00_Introduction.md} +0 -0
- data/spec/support/shared.rb +14 -10
- data/templates/Gemfile +3 -3
- data/templates/Guardfile +1 -5
- data/templates/config.erb +5 -5
- data/templates/cover.erb +4 -3
- data/templates/epub.erb +3 -2
- data/templates/helper.rb +28 -29
- data/templates/images/.gitkeep +0 -0
- data/templates/images/kitabu-icon.png +0 -0
- data/templates/images/kitabu-icon.svg +19 -0
- data/templates/images/kitabu-word.png +0 -0
- data/templates/images/kitabu-word.svg +14 -0
- data/templates/images/kitabu.png +0 -0
- data/templates/images/kitabu.svg +20 -0
- data/{examples/RailsGuides/templates → templates/templates/epub}/cover.erb +4 -3
- data/templates/templates/epub/cover.png +0 -0
- data/templates/templates/epub/page.erb +16 -0
- data/templates/{layout.erb → templates/html/layout.erb} +22 -11
- data/templates/templates/styles/epub.scss +1 -0
- data/templates/templates/styles/files/_normalize.scss +427 -0
- data/templates/templates/styles/html.scss +252 -0
- data/templates/templates/styles/pdf.scss +371 -0
- data/templates/templates/styles/print.scss +2 -0
- data/templates/text/01_Getting_Started.md +26 -0
- data/templates/text/02_Creating_Chapters.md +22 -0
- data/templates/text/03_Syntax_Highlighting.erb +69 -0
- data/templates/text/04_Dynamic_Content.erb +64 -0
- data/templates/text/05_Exporting_Files.md +49 -0
- metadata +143 -83
- data/README.rdoc +0 -218
- data/examples/RailsGuides/config/helper.rb +0 -29
- data/examples/RailsGuides/config/kitabu.yml +0 -44
- data/examples/RailsGuides/images/challenge.png +0 -0
- data/examples/RailsGuides/images/posts_index.png +0 -0
- data/examples/RailsGuides/images/rails_welcome.png +0 -0
- data/examples/RailsGuides/output/RailsGuides.epub +0 -0
- data/examples/RailsGuides/output/RailsGuides.html +0 -1556
- data/examples/RailsGuides/output/RailsGuides.pdf +3 -4934
- data/examples/RailsGuides/templates/layout.css +0 -352
- data/examples/RailsGuides/templates/layout.erb +0 -43
- data/examples/RailsGuides/templates/syntax.css +0 -62
- data/examples/RailsGuides/templates/user.css +0 -19
- data/examples/RailsGuides/text/01_Guide_Assumptions.mkdn +0 -13
- data/examples/RailsGuides/text/02_What_is_Rails.mkdn +0 -106
- data/examples/RailsGuides/text/03_Creating_a_new_Rails_project.mkdn +0 -200
- data/examples/RailsGuides/text/04_Hello_Rails.mkdn +0 -62
- data/examples/RailsGuides/text/05_Getting_Up_and_Running_Quickly_with_Scaffolding.mkdn +0 -4
- data/examples/RailsGuides/text/06_Creating_a_resource.mkdn +0 -503
- data/examples/RailsGuides/text/07_Adding_a_second_model.mkdn +0 -232
- data/examples/RailsGuides/text/08_Refactoring.mkdn +0 -123
- data/examples/RailsGuides/text/09_Deleting_comments.mkdn +0 -57
- data/examples/RailsGuides/text/09_Security.mkdn +0 -56
- data/examples/RailsGuides/text/10_Building_a_multi_model_form.mkdn +0 -130
- data/examples/RailsGuides/text/11_View_helpers.mkdn +0 -50
- data/examples/RailsGuides/text/12_Whats_next.mkdn +0 -14
- data/examples/RailsGuides/text/13_Configuration_gotchas.mkdn +0 -10
- data/lib/kitabu/adapters/markdown.rb +0 -34
- data/lib/kitabu/extensions/redcloth.rb +0 -69
- data/lib/kitabu/syntax.rb +0 -130
- data/spec/kitabu/extensions/redcloth_spec.rb +0 -57
- data/spec/kitabu/syntax_spec.rb +0 -106
- data/spec/support/mybook/templates/html/layout.css +0 -353
- data/spec/support/mybook/templates/html/syntax.css +0 -58
- data/spec/support/mybook/templates/html/user.css +0 -1
- data/spec/support/mybook/text/02_Textile_Chapter.textile +0 -3
- data/spec/support/mybook/text/03_HTML_Chapter.html +0 -3
- data/templates/epub.css +0 -500
- data/templates/layout.css +0 -353
- data/templates/sample.md +0 -6
- data/templates/syntax.css +0 -58
- data/templates/user.css +0 -1
@@ -1,19 +0,0 @@
|
|
1
|
-
/* add custom CSS */
|
2
|
-
|
3
|
-
.notice {
|
4
|
-
background: #cde2f5;
|
5
|
-
border-radius: 5px;
|
6
|
-
padding: 10pt;
|
7
|
-
}
|
8
|
-
|
9
|
-
div.frontcover h1 {
|
10
|
-
font-size: 38pt;
|
11
|
-
}
|
12
|
-
|
13
|
-
div.table-of-contents div.level3 {
|
14
|
-
display: none;
|
15
|
-
}
|
16
|
-
|
17
|
-
div.table-of-contents div.level2 a {
|
18
|
-
font-weight: normal;
|
19
|
-
}
|
@@ -1,13 +0,0 @@
|
|
1
|
-
## Guide Assumptions
|
2
|
-
|
3
|
-
This guide is designed for beginners who want to get started with a Rails application from scratch. It does not assume that you have any prior experience with Rails. However, to get the most out of it, you need to have some prerequisites installed:
|
4
|
-
|
5
|
-
* The [Ruby](http://www.ruby-lang.org/en/downloads) language version 1.8.7 or higher
|
6
|
-
* The [RubyGems](http://rubyforge.org/frs/?group_id=126) packaging system
|
7
|
-
* A working installation of the [SQLite3](http://www.sqlite.org/) Database
|
8
|
-
|
9
|
-
Rails is a web application framework running on the Ruby programming language. If you have no prior experience with Ruby, you will find a very steep learning curve diving straight into Rails. There are some good free resources on the internet for learning Ruby, including:
|
10
|
-
|
11
|
-
* [Mr. Neighborly’s Humble Little Ruby Book](http://www.humblelittlerubybook.com/)
|
12
|
-
* [Programming Ruby](http://www.ruby-doc.org/docs/ProgrammingRuby/)
|
13
|
-
* [Why's (Poignant) Guide to Ruby](http://mislav.uniqpath.com/poignant-guide/)
|
@@ -1,106 +0,0 @@
|
|
1
|
-
## What is Rails?
|
2
|
-
|
3
|
-
Rails is a web application development framework written in the Ruby language. It is designed to make programming web applications easier by making assumptions about what every developer needs to get started. It allows you to write less code while accomplishing more than many other languages and frameworks. Experienced Rails developers also report that it makes web application development more fun.
|
4
|
-
|
5
|
-
Rails is opinionated software. It makes the assumption that there is a “best” way to do things, and it’s designed to encourage that way – and in some cases to discourage alternatives. If you learn “The Rails Way” you’ll probably discover a tremendous increase in productivity. If you persist in bringing old habits from other languages to your Rails development, and trying to use patterns you learned elsewhere, you may have a less happy experience.
|
6
|
-
|
7
|
-
The Rails philosophy includes several guiding principles:
|
8
|
-
|
9
|
-
* DRY – "Don't Repeat Yourself" – suggests that writing the same code over and over again is a bad thing.
|
10
|
-
* Convention Over Configuration – means that Rails makes assumptions about what you want to do and how you’re going to do it, rather than requiring you to specify every little thing through endless configuration files.
|
11
|
-
* REST is the best pattern for web applications – organizing your application around resources and standard HTTP verbs is the fastest way to go.
|
12
|
-
|
13
|
-
### The MVC Architecture
|
14
|
-
|
15
|
-
At the core of Rails is the Model, View, Controller architecture, usually just called MVC. MVC benefits include:
|
16
|
-
|
17
|
-
* Isolation of business logic from the user interface
|
18
|
-
* Ease of keeping code DRY
|
19
|
-
* Making it clear where different types of code belong for easier maintenance
|
20
|
-
|
21
|
-
### Models
|
22
|
-
|
23
|
-
A model represents the information (data) of the application and the rules to manipulate that data. In the case of Rails, models are primarily used for managing the rules of interaction with a corresponding database table. In most cases, one table in your database will correspond to one model in your application. The bulk of your application’s business logic will be concentrated in the models.
|
24
|
-
|
25
|
-
### Views
|
26
|
-
|
27
|
-
Views represent the user interface of your application. In Rails, views are often HTML files with embedded Ruby code that perform tasks related solely to the presentation of the data. Views handle the job of providing data to the web browser or other tool that is used to make requests from your application.
|
28
|
-
|
29
|
-
### Controllers
|
30
|
-
|
31
|
-
Controllers provide the “glue” between models and views. In Rails, controllers are responsible for processing the incoming requests from the web browser, interrogating the models for data, and passing that data on to the views for presentation.
|
32
|
-
|
33
|
-
### The Components of Rails
|
34
|
-
|
35
|
-
Rails ships as many individual components.
|
36
|
-
|
37
|
-
* Action Pack
|
38
|
-
* Action Controller
|
39
|
-
* Action Dispatch
|
40
|
-
* Action View
|
41
|
-
* Action Mailer
|
42
|
-
* Active Model
|
43
|
-
* Active Record
|
44
|
-
* Active Resource
|
45
|
-
* Active Support
|
46
|
-
* Railties
|
47
|
-
|
48
|
-
### Action Pack
|
49
|
-
|
50
|
-
Action Pack is a single gem that contains Action Controller, Action View and Action Dispatch. The "VC" part of "MVC".
|
51
|
-
|
52
|
-
### Action Controller
|
53
|
-
|
54
|
-
Action Controller is the component that manages the controllers in a Rails application. The Action Controller framework processes incoming requests to a Rails application, extracts parameters, and dispatches them to the intended action. Services provided by Action Controller include session management, template rendering, and redirect management.
|
55
|
-
|
56
|
-
### Action View
|
57
|
-
|
58
|
-
Action View manages the views of your Rails application. It can create both HTML and XML output by default. Action View manages rendering templates, including nested and partial templates, and includes built-in AJAX support.
|
59
|
-
|
60
|
-
### Action Dispatch
|
61
|
-
|
62
|
-
Action Dispatch handles routing of web requests and dispatches them as you want, either to your application or any other Rack application.
|
63
|
-
|
64
|
-
### Action Mailer
|
65
|
-
|
66
|
-
Action Mailer is a framework for building e-mail services. You can use Action Mailer to receive and process incoming email and send simple plain text or complex multipart emails based on flexible templates.
|
67
|
-
|
68
|
-
### Active Model
|
69
|
-
|
70
|
-
Active Model provides a defined interface between the Action Pack gem services and Object Relationship Mapping gems such as Active Record. Active Model allows Rails to utilize other ORM frameworks in place of Active Record if your application needs this.
|
71
|
-
|
72
|
-
### Active Record
|
73
|
-
|
74
|
-
Active Record is the base for the models in a Rails application. It provides database independence, basic CRUD functionality, advanced finding capabilities, and the ability to relate models to one another, among other services.
|
75
|
-
|
76
|
-
### Active Resource
|
77
|
-
|
78
|
-
Active Resource provides a framework for managing the connection between business objects and RESTful web services. It implements a way to map web-based resources to local objects with CRUD semantics.
|
79
|
-
|
80
|
-
### Active Support
|
81
|
-
|
82
|
-
Active Support is an extensive collection of utility classes and standard Ruby library extensions that are used in the Rails, both by the core code and by your applications.
|
83
|
-
|
84
|
-
### Railties
|
85
|
-
|
86
|
-
Railties is the core Rails code that builds new Rails applications and glues the various frameworks and plugins together in any Rails application.
|
87
|
-
|
88
|
-
### REST
|
89
|
-
|
90
|
-
Rest stands for Representational State Transfer and is the foundation of the RESTful architecture. This is generally considered to be Roy Fielding’s doctoral thesis, [Architectural Styles and the Design of Network-based Software Architectures](http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm). While you can read through the thesis, REST in terms of Rails boils down to two main principles:
|
91
|
-
|
92
|
-
* Using resource identifiers such as URLs to represent resources.
|
93
|
-
* Transferring representations of the state of that resource between system components.
|
94
|
-
|
95
|
-
For example, to a Rails application a request such as this:
|
96
|
-
|
97
|
-
DELETE /photos/17
|
98
|
-
|
99
|
-
would be understood to refer to a photo resource with the ID of 17, and to indicate a desired action – deleting that resource. REST is a natural style for the architecture of web applications, and Rails hooks into this shielding you from many of the RESTful complexities and browser quirks.
|
100
|
-
|
101
|
-
If you’d like more details on REST as an architectural style, these resources are more approachable than Fielding’s thesis:
|
102
|
-
|
103
|
-
* [A Brief Introduction to REST](http://www.infoq.com/articles/rest-introduction) by Stefan Tilkov
|
104
|
-
* [An Introduction to REST](http://bitworking.org/news/373/An-Introduction-to-REST) (video tutorial) by Joe Gregorio
|
105
|
-
* [Representational State Transfer](http://en.wikipedia.org/wiki/Representational_State_Transfer) article in Wikipedia
|
106
|
-
* [How to GET a Cup of Coffee](http://www.infoq.com/articles/webber-rest-workflow) by Jim Webber, Savas Parastatidis & Ian Robinson
|
@@ -1,200 +0,0 @@
|
|
1
|
-
## Creating a New Rails Project
|
2
|
-
|
3
|
-
If you follow this guide, you’ll create a Rails project called blog, a (very) simple weblog. Before you can start building the application, you need to make sure that you have Rails itself installed.
|
4
|
-
|
5
|
-
### Installing Rails
|
6
|
-
|
7
|
-
In most cases, the easiest way to install Rails is to take advantage of RubyGems:
|
8
|
-
|
9
|
-
Usually run this as the root user:
|
10
|
-
# gem install rails
|
11
|
-
|
12
|
-
<p class="notice">
|
13
|
-
If you’re working on Windows, you should be aware that the vast majority of Rails development is done in Unix environments. While Ruby and Rails themselves install easily using for example Ruby Installer, the supporting ecosystem often assumes you are able to build C-based rubygems and work in a command window. If at all possible, we suggest that you install a Linux virtual machine and use that for Rails development, instead of using Windows.
|
14
|
-
</p>
|
15
|
-
|
16
|
-
### Creating the Blog Application
|
17
|
-
|
18
|
-
The best way to use this guide is to follow each step as it happens, no code or step needed to make this example application has been left out, so you can literally follow along step by step. If you need to see the completed code, you can download it from [Getting Started Code](http://github.com/mikel/getting-started-code).
|
19
|
-
|
20
|
-
To begin, open a terminal, navigate to a folder where you have rights to create files, and type:
|
21
|
-
|
22
|
-
$ rails new blog
|
23
|
-
|
24
|
-
This will create a Rails application called Blog in a directory called blog.
|
25
|
-
|
26
|
-
<p class="notice">
|
27
|
-
You can see all of the switches that the Rails application builder accepts by running <code>rails -h</code>.
|
28
|
-
</p>
|
29
|
-
|
30
|
-
After you create the blog application, switch to its folder to continue work directly in that application:
|
31
|
-
|
32
|
-
$ cd blog
|
33
|
-
|
34
|
-
In any case, Rails will create a folder in your working directory called `blog`. Open up that folder and explore its contents. Most of the work in this tutorial will happen in the `app/` folder, but here’s a basic rundown on the function of each folder that Rails creates in a new application by default:
|
35
|
-
|
36
|
-
<table>
|
37
|
-
<thead>
|
38
|
-
<tr>
|
39
|
-
<th>File/Folder </th>
|
40
|
-
<th>Purpose</th>
|
41
|
-
</tr>
|
42
|
-
</thead>
|
43
|
-
|
44
|
-
<tbody>
|
45
|
-
<tr>
|
46
|
-
<td>Gemfile</td>
|
47
|
-
<td>This file allows you to specify what gem dependencies are needed for your Rails application.</td>
|
48
|
-
</tr>
|
49
|
-
|
50
|
-
<tr>
|
51
|
-
<td>README.rdoc</td>
|
52
|
-
<td>This is a brief instruction manual for your application. Use it to tell others what your application does, how to set it up, and so on.</td>
|
53
|
-
</tr>
|
54
|
-
|
55
|
-
<tr>
|
56
|
-
<td>Rakefile</td>
|
57
|
-
<td>This file contains batch jobs that can be run from the terminal.</td>
|
58
|
-
</tr>
|
59
|
-
|
60
|
-
<tr>
|
61
|
-
<td>app/</td>
|
62
|
-
<td>Contains the controllers, models, and views for your application. You’ll focus on this folder for the remainder of this guide.</td>
|
63
|
-
</tr>
|
64
|
-
|
65
|
-
<tr>
|
66
|
-
<td>config/</td>
|
67
|
-
<td>Configure your application’s runtime rules, routes, database, and more.</td>
|
68
|
-
</tr>
|
69
|
-
|
70
|
-
<tr>
|
71
|
-
<td>config.ru</td>
|
72
|
-
<td>Rack configuration for Rack based servers used to start the application.</td>
|
73
|
-
</tr>
|
74
|
-
|
75
|
-
<tr>
|
76
|
-
<td>db/</td>
|
77
|
-
<td>Shows your current database schema, as well as the database migrations. You’ll learn about migrations shortly.</td>
|
78
|
-
</tr>
|
79
|
-
|
80
|
-
<tr>
|
81
|
-
<td>doc/</td>
|
82
|
-
<td>In-depth documentation for your application.</td>
|
83
|
-
</tr>
|
84
|
-
|
85
|
-
<tr>
|
86
|
-
<td>lib/</td>
|
87
|
-
<td>Extended modules for your application (not covered in this guide).</td>
|
88
|
-
</tr>
|
89
|
-
|
90
|
-
<tr>
|
91
|
-
<td>log/</td>
|
92
|
-
<td>Application log files.</td>
|
93
|
-
</tr>
|
94
|
-
|
95
|
-
<tr>
|
96
|
-
<td>public/</td>
|
97
|
-
<td>The only folder seen to the world as-is. This is where your images, javascript, stylesheets (CSS), and other static files go.</td>
|
98
|
-
</tr>
|
99
|
-
|
100
|
-
<tr>
|
101
|
-
<td>script/</td>
|
102
|
-
<td>Contains the rails script that starts your app and can contain other scripts you use to deploy or run your application.</td>
|
103
|
-
</tr>
|
104
|
-
|
105
|
-
<tr>
|
106
|
-
<td>test/</td>
|
107
|
-
<td>Unit tests, fixtures, and other test apparatus.</td>
|
108
|
-
</tr>
|
109
|
-
|
110
|
-
<tr>
|
111
|
-
<td>tmp/</td>
|
112
|
-
<td>Temporary files.</td>
|
113
|
-
</tr>
|
114
|
-
|
115
|
-
<tr>
|
116
|
-
<td>vendor/</td>
|
117
|
-
<td>A place for all third-party code. In a typical Rails application, this includes Ruby Gems, the Rails source code (if you install it into your project) and plugins containing additional prepackaged functionality.</td>
|
118
|
-
</tr>
|
119
|
-
|
120
|
-
</tbody>
|
121
|
-
</table>
|
122
|
-
|
123
|
-
### Installing the Required Gems
|
124
|
-
|
125
|
-
Rails applications manage gem dependencies with [Bundler](http://www.github.com/carlhuda/bundler) by default. As we don’t need any other gems beyond the ones in the generated `Gemfile` we can directly run
|
126
|
-
|
127
|
-
bundle install
|
128
|
-
|
129
|
-
to have them ready.
|
130
|
-
|
131
|
-
### Configuring a Database
|
132
|
-
|
133
|
-
Just about every Rails application will interact with a database. The database to use is specified in a configuration file, `config/database.yml`. If you open this file in a new Rails application, you’ll see a default database configuration using SQLite3. The file contains sections for three different environments in which Rails can run by default:
|
134
|
-
|
135
|
-
* The `development` environment is used on your development computer as you interact manually with the application
|
136
|
-
* The `test` environment is used to run automated tests
|
137
|
-
* The `production` environment is used when you deploy your application for the world to use.
|
138
|
-
|
139
|
-
### Configuring an SQLite3 Database
|
140
|
-
|
141
|
-
Rails comes with built-in support for [SQLite3](http://www.sqlite.org/), which is a lightweight serverless database application. While a busy production environment may overload SQLite, it works well for development and testing. Rails defaults to using an SQLite database when creating a new project, but you can always change it later.
|
142
|
-
|
143
|
-
Here’s the section of the default configuration file (`config/database.yml`) with connection information for the development environment:
|
144
|
-
|
145
|
-
@@@ yaml
|
146
|
-
development:
|
147
|
-
adapter: sqlite3
|
148
|
-
database: db/development.sqlite3
|
149
|
-
pool: 5
|
150
|
-
timeout: 5000
|
151
|
-
@@@
|
152
|
-
|
153
|
-
<p class="notice">
|
154
|
-
In this guide we are using an SQLite3 database for data storage, because it is a zero configuration database that just works. Rails also supports MySQL and PostgreSQL “out of the box”, and has plugins for many database systems. If you are using a database in a production environment Rails most likely has an adapter for it.
|
155
|
-
</p>
|
156
|
-
|
157
|
-
### Configuring a MySQL Database
|
158
|
-
|
159
|
-
If you choose to use MySQL instead of the shipped Sqlite3 database, your `config/database.yml` will look a little different. Here’s the development section:
|
160
|
-
|
161
|
-
@@@ yaml
|
162
|
-
development:
|
163
|
-
adapter: mysql2
|
164
|
-
encoding: utf8
|
165
|
-
database: blog_development
|
166
|
-
pool: 5
|
167
|
-
username: root
|
168
|
-
password:
|
169
|
-
socket: /tmp/mysql.sock
|
170
|
-
@@@
|
171
|
-
|
172
|
-
If your development computer’s MySQL installation includes a root user with an empty password, this configuration should work for you. Otherwise, change the username and password in the `development` section as appropriate.
|
173
|
-
|
174
|
-
### Configuring a PostgreSQL Database
|
175
|
-
|
176
|
-
Finally if you choose to use PostgreSQL, your config/database.yml will be customized to use PostgreSQL databases:
|
177
|
-
|
178
|
-
@@@ yaml
|
179
|
-
development:
|
180
|
-
adapter: postgresql
|
181
|
-
encoding: unicode
|
182
|
-
database: blog_development
|
183
|
-
pool: 5
|
184
|
-
username: blog
|
185
|
-
password:
|
186
|
-
@@@
|
187
|
-
|
188
|
-
Change the username and password in the `development` section as appropriate.
|
189
|
-
|
190
|
-
### Creating the Database
|
191
|
-
|
192
|
-
Now that you have your database configured, it’s time to have Rails create an empty database for you. You can do this by running a rake command:
|
193
|
-
|
194
|
-
$ rake db:create
|
195
|
-
|
196
|
-
This will create your development and test SQLite3 databases inside the `db/` folder.
|
197
|
-
|
198
|
-
<p class="notice">
|
199
|
-
Rake is a general-purpose command-runner that Rails uses for many things. You can see the list of available rake commands in your application by running <code>rake -T</code>.
|
200
|
-
</p>
|
@@ -1,62 +0,0 @@
|
|
1
|
-
## Hello, Rails!
|
2
|
-
|
3
|
-
One of the traditional places to start with a new language is by getting some text up on screen quickly, to do this, you need to get your Rails application server running.
|
4
|
-
|
5
|
-
### Starting up the Web Server
|
6
|
-
|
7
|
-
<p class="figure">
|
8
|
-
<img src="../images/rails_welcome.png" alt="Rails Welcome page" />
|
9
|
-
</p>
|
10
|
-
|
11
|
-
You actually have a functional Rails application already. To see it, you need to start a web server on your development machine. You can do this by running:
|
12
|
-
|
13
|
-
$ rails server
|
14
|
-
|
15
|
-
This will fire up an instance of the Mongrel web server by default (Rails can also use several other web servers). To see your application in action, open a browser window and navigate to <http://localhost:3000>. You should see Rails’ default information page:
|
16
|
-
|
17
|
-
To stop the web server, hit `Ctrl+C` in the terminal window where it’s running. In development mode, Rails does not generally require you to stop the server; changes you make in files will be automatically picked up by the server.
|
18
|
-
|
19
|
-
The “Welcome Aboard” page is the *smoke test* for a new Rails application: it makes sure that you have your software configured correctly enough to serve a page. You can also click on the *About your application’s environment* link to see a summary of your Application’s environment.
|
20
|
-
|
21
|
-
### Say “Hello”, Rails
|
22
|
-
|
23
|
-
To get Rails saying “Hello”, you need to create at minimum a controller and a view. Fortunately, you can do that in a single command. Enter this command in your terminal:
|
24
|
-
|
25
|
-
$ rails generate controller home index
|
26
|
-
|
27
|
-
<p class="notice">
|
28
|
-
If you’re on Windows, or your Ruby is set up in some non-standard fashion, you may need to explicitly pass Rails <code>rails</code> commands to Ruby: <code>ruby \path\to\rails controller home index</code>.
|
29
|
-
</p>
|
30
|
-
|
31
|
-
Rails will create several files for you, including `app/views/home/index.html.erb`. This is the template that will be used to display the results of the index action (method) in the home controller. Open this file in your text editor and edit it to contain a single line of code:
|
32
|
-
|
33
|
-
@@@ html
|
34
|
-
<h1>Hello, Rails!</h1>
|
35
|
-
@@@
|
36
|
-
|
37
|
-
### Setting the Application Home Page
|
38
|
-
|
39
|
-
Now that we have made the controller and view, we need to tell Rails when we want “Hello Rails” to show up. In our case, we want it to show up when we navigate to the root URL of our site, <http://localhost:3000>, instead of the “Welcome Aboard” smoke test.
|
40
|
-
|
41
|
-
The first step to doing this is to delete the default page from your application:
|
42
|
-
|
43
|
-
$ rm public/index.html
|
44
|
-
|
45
|
-
We need to do this as Rails will deliver any static file in the `public` directory in preference to any dynamic contact we generate from the controllers.
|
46
|
-
|
47
|
-
Now, you have to tell Rails where your actual home page is located. Open the file `config/routes.rb` in your editor. This is your application’s routing file which holds entries in a special DSL (domain-specific language) that tells Rails how to connect incoming requests to controllers and actions. This file contains many sample routes on commented lines, and one of them actually shows you how to connect the root of your site to a specific controller and action. Find the line beginning with :root to, uncomment it and change it like the following:
|
48
|
-
|
49
|
-
@@@ ruby
|
50
|
-
Blog::Application.routes.draw do
|
51
|
-
|
52
|
-
#...
|
53
|
-
# You can have the root of your site routed with "root"
|
54
|
-
# just remember to delete public/index.html.
|
55
|
-
root :to => "home#index"
|
56
|
-
@@@
|
57
|
-
|
58
|
-
The `root :to => "home#index"` tells Rails to map the root action to the home controller’s index action.
|
59
|
-
|
60
|
-
Now if you navigate to <http://localhost:3000> in your browser, you’ll see `Hello, Rails!`.
|
61
|
-
|
62
|
-
For more information about routing, refer to [Rails Routing from the Outside In](http://guides.rubyonrails.org/routing.html).
|
@@ -1,4 +0,0 @@
|
|
1
|
-
## Getting Up and Running Quickly with Scaffolding
|
2
|
-
|
3
|
-
Rails *scaffolding* is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.
|
4
|
-
|
@@ -1,503 +0,0 @@
|
|
1
|
-
## Creating a Resource
|
2
|
-
|
3
|
-
In the case of the blog application, you can start by generating a scaffolded Post resource: this will represent a single blog posting. To do this, enter this command in your terminal:
|
4
|
-
|
5
|
-
$ rails generate scaffold Post name:string title:string content:text
|
6
|
-
|
7
|
-
<p class="notice">
|
8
|
-
While scaffolding will get you up and running quickly, the “one size fits all” code that it generates is unlikely to be a perfect fit for your application. In most cases, you’ll need to customize the generated code. Many experienced Rails developers avoid scaffolding entirely, preferring to write all or most of their source code from scratch.
|
9
|
-
</p>
|
10
|
-
|
11
|
-
The scaffold generator will build 15 files in your application, along with some folders, and edit one more. Here’s a quick overview of what it creates:
|
12
|
-
|
13
|
-
<table>
|
14
|
-
<thead>
|
15
|
-
<tr>
|
16
|
-
<th>File</th>
|
17
|
-
<th>Purpose</th>
|
18
|
-
</tr>
|
19
|
-
</thead>
|
20
|
-
|
21
|
-
<tbody>
|
22
|
-
<tr>
|
23
|
-
<td>db/migrate/20100207214725_create_posts.rb.rb</td>
|
24
|
-
<td>Migration to create the posts table in your database (your name will include a different timestamp)</td>
|
25
|
-
</tr>
|
26
|
-
|
27
|
-
<tr>
|
28
|
-
<td>app/models/post.rb</td>
|
29
|
-
<td>The Post model</td>
|
30
|
-
</tr>
|
31
|
-
|
32
|
-
<tr>
|
33
|
-
<td>test/fixtures/posts.yml</td>
|
34
|
-
<td>Dummy posts for use in testing</td>
|
35
|
-
</tr>
|
36
|
-
|
37
|
-
<tr>
|
38
|
-
<td>app/controllers/posts_controller.rb</td>
|
39
|
-
<td>The Posts controller</td>
|
40
|
-
</tr>
|
41
|
-
|
42
|
-
<tr>
|
43
|
-
<td>app/views/posts/index.html.erb</td>
|
44
|
-
<td>A view to display an index of all posts</td>
|
45
|
-
</tr>
|
46
|
-
|
47
|
-
<tr>
|
48
|
-
<td>db/migrate/20100207214725_create_posts.rb</td>
|
49
|
-
<td>Migration to create the posts table in your database (your name will include a different timestamp)
|
50
|
-
</td>
|
51
|
-
</tr>
|
52
|
-
|
53
|
-
<tr>
|
54
|
-
<td>app/views/posts/edit.html.erb</td>
|
55
|
-
<td>A view to edit an existing post</td>
|
56
|
-
</tr>
|
57
|
-
|
58
|
-
<tr>
|
59
|
-
<td>app/views/posts/show.html.erb</td>
|
60
|
-
<td>A view to display a single post</td>
|
61
|
-
</tr>
|
62
|
-
|
63
|
-
<tr>
|
64
|
-
<td>app/views/posts/_form.html.erb</td>
|
65
|
-
<td>A partial to control the overall look and feel of the form used in edit and new views</td>
|
66
|
-
</tr>
|
67
|
-
|
68
|
-
<tr>
|
69
|
-
<td>app/helpers/posts_helper.rb</td>
|
70
|
-
<td>Helper functions to be used from the post views</td>
|
71
|
-
</tr>
|
72
|
-
|
73
|
-
<tr>
|
74
|
-
<td>test/unit/post_test.rb</td>
|
75
|
-
<td>Unit testing harness for the posts model</td>
|
76
|
-
</tr>
|
77
|
-
|
78
|
-
<tr>
|
79
|
-
<td>test/functional/posts_controller_test.rb</td>
|
80
|
-
<td>Functional testing harness for the posts controller</td>
|
81
|
-
</tr>
|
82
|
-
|
83
|
-
<tr>
|
84
|
-
<td>test/unit/helpers/posts_helper_test.rb</td>
|
85
|
-
<td>Unit testing harness for the posts helper</td>
|
86
|
-
</tr>
|
87
|
-
|
88
|
-
<tr>
|
89
|
-
<td>config/routes.rb</td>
|
90
|
-
<td>Edited to include routing information for posts</td>
|
91
|
-
</tr>
|
92
|
-
|
93
|
-
<tr>
|
94
|
-
<td>public/stylesheets/scaffold.css</td>
|
95
|
-
<td>Cascading style sheet to make the scaffolded views look better</td>
|
96
|
-
</tr>
|
97
|
-
</tbody>
|
98
|
-
</table>
|
99
|
-
|
100
|
-
### Running a Migration
|
101
|
-
|
102
|
-
One of the products of the `rails generate scaffold` command is a database *migration*. Migrations are Ruby classes that are designed to make it simple to create and modify database tables. Rails uses rake commands to run migrations, and it’s possible to undo a migration after it’s been applied to your database. Migration filenames include a timestamp to ensure that they’re processed in the order that they were created.
|
103
|
-
|
104
|
-
If you look in the `db/migrate/20100207214725_create_posts.rb` file (remember, yours will have a slightly different name), here’s what you’ll find:
|
105
|
-
|
106
|
-
@@@ ruby
|
107
|
-
class CreatePosts < ActiveRecord::Migration
|
108
|
-
def self.up
|
109
|
-
create_table :posts do |t|
|
110
|
-
t.string :name
|
111
|
-
t.string :title
|
112
|
-
t.text :content
|
113
|
-
|
114
|
-
t.timestamps
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
def self.down
|
119
|
-
drop_table :posts
|
120
|
-
end
|
121
|
-
end
|
122
|
-
@@@
|
123
|
-
|
124
|
-
The above migration creates two methods, `up`, called when you run this migration into the database, and `down` in case you need to reverse the changes made by this migration at a later date. The up command in this case creates a posts table with two string columns and a text column. It also creates two timestamp fields to track record creation and updating. More information about Rails migrations can be found in the [Rails Database Migrations](http://guides.rubyonrails.org/migrations.html) guide.
|
125
|
-
|
126
|
-
At this point, you can use a rake command to run the migration:
|
127
|
-
|
128
|
-
$ rake db:migrate
|
129
|
-
|
130
|
-
Rails will execute this migration command and tell you it created the Posts table.
|
131
|
-
|
132
|
-
== CreatePosts: migrating ===========
|
133
|
-
-- create_table(:posts)
|
134
|
-
-> 0.0019s
|
135
|
-
== CreatePosts: migrated (0.0020s) ==
|
136
|
-
|
137
|
-
<p class="notice">
|
138
|
-
Because you’re working in the development environment by default, this command will apply to the database defined in the <code>development</code> section of your <code>config/database.yml</code> file.
|
139
|
-
</p>
|
140
|
-
|
141
|
-
### Adding a Link
|
142
|
-
|
143
|
-
To hook the posts up to the home page you’ve already created, you can add a link to the home page. Open `app/views/home/index.html.erb` and modify it as follows:
|
144
|
-
|
145
|
-
<h1>Hello, Rails!</h1> <%= link_to "My Blog", posts_path %>
|
146
|
-
|
147
|
-
The `link_to` method is one of Rails’ built-in view helpers. It creates a hyperlink based on text to display and where to go – in this case, to the path for posts.
|
148
|
-
|
149
|
-
### Working with Posts in the Browser
|
150
|
-
|
151
|
-
<p class="figure">
|
152
|
-
<img src="../images/posts_index.png" alt="Post index page" />
|
153
|
-
</p>
|
154
|
-
|
155
|
-
Now you’re ready to start working with posts. To do that, navigate to <http://localhost:3000> and then click the “My Blog” link:
|
156
|
-
|
157
|
-
This is the result of Rails rendering the `index` view of your posts. There aren’t currently any posts in the database, but if you click the `New Post` link you can create one. After that, you’ll find that you can edit posts, look at their details, or destroy them. All of the logic and HTML to handle this was built by the single `rails generate scaffold` command.
|
158
|
-
|
159
|
-
<p class="notice">
|
160
|
-
In development mode (which is what you’re working in by default), Rails reloads your application with every browser request, so there’s no need to stop and restart the web server.
|
161
|
-
</p>
|
162
|
-
|
163
|
-
Congratulations, you’re riding the rails! Now it’s time to see how it all works.
|
164
|
-
|
165
|
-
### The Model
|
166
|
-
|
167
|
-
The model file, `app/models/post.rb` is about as simple as it can get:
|
168
|
-
|
169
|
-
@@@ ruby
|
170
|
-
class Post < ActiveRecord::Base
|
171
|
-
end
|
172
|
-
@@@
|
173
|
-
|
174
|
-
There isn’t much to this file – but note that the `Post` class inherits from `ActiveRecord::Base`. Active Record supplies a great deal of functionality to your Rails models for free, including basic database CRUD (Create, Read, Update, Destroy) operations, data validation, as well as sophisticated search support and the ability to relate multiple models to one another.
|
175
|
-
|
176
|
-
### Adding Some Validation
|
177
|
-
|
178
|
-
Rails includes methods to help you validate the data that you send to models. Open the `app/models/post.rb` file and edit it:
|
179
|
-
|
180
|
-
@@@ ruby
|
181
|
-
class Post < ActiveRecord::Base
|
182
|
-
validates :name, :presence => true
|
183
|
-
validates :title, :presence => true,
|
184
|
-
:length => { :minimum => 5 }
|
185
|
-
end
|
186
|
-
@@@
|
187
|
-
|
188
|
-
These changes will ensure that all posts have a name and a title, and that the title is at least five characters long. Rails can validate a variety of conditions in a model, including the presence or uniqueness of columns, their format, and the existence of associated objects.
|
189
|
-
|
190
|
-
### Using the Console
|
191
|
-
|
192
|
-
To see your validations in action, you can use the console. The console is a command-line tool that lets you execute Ruby code in the context of your application:
|
193
|
-
|
194
|
-
$ rails console
|
195
|
-
|
196
|
-
After the console loads, you can use it to work with your application’s models:
|
197
|
-
|
198
|
-
@@@ text
|
199
|
-
>> p = Post.new(:content => "A new post")
|
200
|
-
=> #<Post id: nil, name: nil, title: nil,
|
201
|
-
content: "A new post", created_at: nil,
|
202
|
-
updated_at: nil>
|
203
|
-
>> p.save
|
204
|
-
=> false
|
205
|
-
>> p.errors
|
206
|
-
=> #<OrderedHash { :title=>["can't be blank",
|
207
|
-
"is too short (minimum is 5 characters)"],
|
208
|
-
:name=>["can't be blank"] }>
|
209
|
-
@@@
|
210
|
-
|
211
|
-
This code shows creating a new `Post` instance, attempting to save it and getting `false` for a return value (indicating that the save failed), and inspecting the errors of the post.
|
212
|
-
|
213
|
-
When you’re finished, type `exit` and hit `return` to exit the console.
|
214
|
-
|
215
|
-
<p class="notice">
|
216
|
-
Unlike the development web server, the console does not automatically load your code afresh for each line. If you make changes to your models while the console is open, type reload! at the console prompt to load them.
|
217
|
-
</p>
|
218
|
-
|
219
|
-
### Listing All Posts
|
220
|
-
|
221
|
-
The easiest place to start looking at functionality is with the code that lists all posts. Open the file `app/controllers/posts_controller.rb` and look at the `index` action:
|
222
|
-
|
223
|
-
@@@ ruby
|
224
|
-
def index
|
225
|
-
@posts = Post.all
|
226
|
-
|
227
|
-
respond_to do |format|
|
228
|
-
format.html # index.html.erb
|
229
|
-
format.xml { render :xml => @posts }
|
230
|
-
end
|
231
|
-
end
|
232
|
-
@@@
|
233
|
-
|
234
|
-
`Post.all` calls the `Post` model to return all of the posts currently in the database. The result of this call is an array containing the posts which has been saved in an instance variable called `@posts`.
|
235
|
-
|
236
|
-
<p class="notice">
|
237
|
-
For more information on finding records with Active Record, see <a href="http://guides.rubyonrails.org/active_record_querying.html" title="Ruby on Rails Guides: Active Record Query Interface">Active Record Query Interface</a>.
|
238
|
-
</p>
|
239
|
-
|
240
|
-
The respond_to block handles both HTML and XML calls to this action. If you browse to <http://localhost:3000/posts.xml>, you’ll see all of the posts in XML format. The HTML format looks for a view in `app/views/posts/` with a name that corresponds to the action name. Rails makes all of the instance variables from the action available to the view. Here’s `app/views/posts/index.html.erb`:
|
241
|
-
|
242
|
-
@@@ html
|
243
|
-
<h1>Listing posts</h1>
|
244
|
-
|
245
|
-
<table>
|
246
|
-
<tr>
|
247
|
-
<th>Name</th>
|
248
|
-
<th>Title</th>
|
249
|
-
<th>Content</th>
|
250
|
-
<th></th>
|
251
|
-
<th></th>
|
252
|
-
<th></th>
|
253
|
-
</tr>
|
254
|
-
|
255
|
-
<% @posts.each do |post| %>
|
256
|
-
<tr>
|
257
|
-
<td><%= post.name %></td>
|
258
|
-
<td><%= post.title %></td>
|
259
|
-
<td><%= post.content %></td>
|
260
|
-
<td><%= link_to 'Show', post %></td>
|
261
|
-
<td><%= link_to 'Edit', edit_post_path(post) %></td>
|
262
|
-
<td><%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %></td>
|
263
|
-
</tr>
|
264
|
-
<% end %>
|
265
|
-
</table>
|
266
|
-
|
267
|
-
<br />
|
268
|
-
|
269
|
-
<%= link_to 'New post', new_post_path %>
|
270
|
-
@@@
|
271
|
-
|
272
|
-
This view iterates over the contents of the `@posts` array to display content and links. A few things to note in the view:
|
273
|
-
|
274
|
-
* `link_to` builds a hyperlink to a particular destination
|
275
|
-
* `edit_post_path` and `new_post_path` are helpers that Rails provides as part of RESTful routing. You’ll see a variety of these helpers for the different actions that the controller includes.
|
276
|
-
|
277
|
-
<p class="notice">
|
278
|
-
In previous versions of Rails, you had to use <code><%=h post.name %></code> so that any HTML would be escaped before being inserted into the page. In Rails 3.0, this is now the default. To get unescaped HTML, you now use <code><%= raw post.name %></code>. For more details on the rendering process, see <a href="http://guides.rubyonrails.org/layouts_and_rendering.html" title="Ruby on Rails Guides: Layouts and Rendering in Rails">Layouts and Rendering in Rails</a>.
|
279
|
-
</p>
|
280
|
-
|
281
|
-
### Customizing the Layout
|
282
|
-
|
283
|
-
The view is only part of the story of how HTML is displayed in your web browser. Rails also has the concept of *layouts*, which are containers for views. When Rails renders a view to the browser, it does so by putting the view’s HTML into a layout’s HTML. In previous versions of Rails, the `rails generate scaffold` command would automatically create a controller specific layout, like `app/views/layouts/posts.html.erb`, for the posts controller. However this has been changed in Rails 3.0. A application specific layout is used for all the controllers and can be found in `app/views/layouts/application.html.erb`. Open this layout in your editor and modify the body tag:
|
284
|
-
|
285
|
-
@@@ html
|
286
|
-
<!DOCTYPE html>
|
287
|
-
<html>
|
288
|
-
<head>
|
289
|
-
<title>Blog</title>
|
290
|
-
<%= stylesheet_link_tag :all %>
|
291
|
-
<%= javascript_include_tag :defaults %>
|
292
|
-
<%= csrf_meta_tag %>
|
293
|
-
</head>
|
294
|
-
<body style="background: #EEEEEE;">
|
295
|
-
|
296
|
-
<%= yield %>
|
297
|
-
|
298
|
-
</body>
|
299
|
-
</html>
|
300
|
-
@@@
|
301
|
-
|
302
|
-
Now when you refresh the `/posts` page, you’ll see a gray background to the page. This same gray background will be used throughout all the views for posts.
|
303
|
-
|
304
|
-
### Creating New Posts
|
305
|
-
|
306
|
-
Creating a new post involves two actions. The first is the `new` action, which instantiates an empty `Post` object:
|
307
|
-
|
308
|
-
@@@ ruby
|
309
|
-
def new
|
310
|
-
@post = Post.new
|
311
|
-
|
312
|
-
respond_to do |format|
|
313
|
-
format.html # new.html.erb
|
314
|
-
format.xml { render :xml => @post }
|
315
|
-
end
|
316
|
-
end
|
317
|
-
@@@
|
318
|
-
|
319
|
-
The `new.html.erb` view displays this empty `Post` to the user:
|
320
|
-
|
321
|
-
@@@ html
|
322
|
-
<h1>New post</h1>
|
323
|
-
|
324
|
-
<%= render 'form' %>
|
325
|
-
|
326
|
-
<%= link_to 'Back', posts_path %>
|
327
|
-
@@@
|
328
|
-
|
329
|
-
The `<%= render 'form' %>` line is our first introduction to *partials* in Rails. A partial is a snippet of HTML and Ruby code that can be reused in multiple locations. In this case, the form used to make a new post, is basically identical to a form used to edit a post, both have text fields for the name and title and a text area for the content with a button to make a new post or update the existing post.
|
330
|
-
|
331
|
-
If you take a look at `views/posts/_form.html.erb` file, you will see the following:
|
332
|
-
|
333
|
-
@@@ html
|
334
|
-
<%= form_for(@post) do |f| %>
|
335
|
-
<% if @post.errors.any? %>
|
336
|
-
<div id="errorExplanation">
|
337
|
-
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
|
338
|
-
<ul>
|
339
|
-
<% @post.errors.full_messages.each do |msg| %>
|
340
|
-
<li><%= msg %></li>
|
341
|
-
<% end %>
|
342
|
-
</ul>
|
343
|
-
</div>
|
344
|
-
<% end %>
|
345
|
-
|
346
|
-
<div class="field">
|
347
|
-
<%= f.label :name %><br />
|
348
|
-
<%= f.text_field :name %>
|
349
|
-
</div>
|
350
|
-
<div class="field">
|
351
|
-
<%= f.label :title %><br />
|
352
|
-
<%= f.text_field :title %>
|
353
|
-
</div>
|
354
|
-
<div class="field">
|
355
|
-
<%= f.label :content %><br />
|
356
|
-
<%= f.text_area :content %>
|
357
|
-
</div>
|
358
|
-
<div class="actions">
|
359
|
-
<%= f.submit %>
|
360
|
-
</div>
|
361
|
-
<% end %>
|
362
|
-
@@@
|
363
|
-
|
364
|
-
This partial receives all the instance variables defined in the calling view file, so in this case, the controller assigned the new `Post` object to `@post` and so, this is available in both the view and partial as `@post`.
|
365
|
-
|
366
|
-
For more information on partials, refer to the [Layouts and Rendering in Rails](http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials) guide.
|
367
|
-
|
368
|
-
The `form_for` block is used to create an HTML form. Within this block, you have access to methods to build various controls on the form. For example, `f.text_field :name` tells Rails to create a text input on the form, and to hook it up to the `name` attribute of the instance being displayed. You can only use these methods with attributes of the model that the form is based on (in this case `name`, `title`, and `content`). Rails uses `form_for` in preference to having you write raw HTML because the code is more succinct, and because it explicitly ties the form to a particular model instance.
|
369
|
-
|
370
|
-
The `form_for` block is also smart enough to work out if you are doing a *New Post* or an *Edit Post* action, and will set the form `action` tags and submit button names appropriately in the HTML output.
|
371
|
-
|
372
|
-
<p class="notice">
|
373
|
-
If you need to create an HTML form that displays arbitrary fields, not tied to a model, you should use the <code>form_tag</code> method, which provides shortcuts for building forms that are not necessarily tied to a model instance.
|
374
|
-
</p>
|
375
|
-
|
376
|
-
When the user clicks the *Create Post* button on this form, the browser will send information back to the create method of the controller (Rails knows to call the `create` method because the form is sent with an HTTP POST request; that’s one of the conventions that I mentioned earlier):
|
377
|
-
|
378
|
-
@@@ ruby
|
379
|
-
def create
|
380
|
-
@post = Post.new(params[:post])
|
381
|
-
|
382
|
-
respond_to do |format|
|
383
|
-
if @post.save
|
384
|
-
format.html { redirect_to(@post,
|
385
|
-
:notice => 'Post was successfully created.') }
|
386
|
-
format.xml { render :xml => @post,
|
387
|
-
:status => :created, :location => @post }
|
388
|
-
else
|
389
|
-
format.html { render :action => "new" }
|
390
|
-
format.xml { render :xml => @post.errors,
|
391
|
-
:status => :unprocessable_entity }
|
392
|
-
end
|
393
|
-
end
|
394
|
-
end
|
395
|
-
@@@
|
396
|
-
|
397
|
-
The `create` action instantiates a new Post object from the data supplied by the user on the form, which Rails makes available in the `params` hash. After successfully saving the new post, `create` returns the appropriate format that the user has requested (HTML in our case). It then redirects the user to the resulting post show action and sets a notice to the user that the Post was successfully created.
|
398
|
-
|
399
|
-
If the post was not successfully saved, due to a validation error, then the controller returns the user back to the new action with any error messages so that the user has the chance to fix the error and try again.
|
400
|
-
|
401
|
-
The “Post was successfully created” message is stored inside of the Rails `flash` hash, (usually just called the Flash) so that messages can be carried over to another action, providing the user with useful information on the status of their request. In the case of `create`, the user never actually sees any page rendered during the Post creation process, because it immediately redirects to the new Post as soon Rails saves the record. The Flash carries over a message to the next action, so that when the user is redirected back to the `show` action, they are presented with a message saying “Post was successfully created.”
|
402
|
-
|
403
|
-
### Showing an Individual Post
|
404
|
-
|
405
|
-
When you click the `show` link for a post on the index page, it will bring you to a URL like `http://localhost:3000/posts/1`. Rails interprets this as a call to the `show` action for the resource, and passes in `1` as the `:id` parameter. Here’s the show action:
|
406
|
-
|
407
|
-
@@@ ruby
|
408
|
-
def show
|
409
|
-
@post = Post.find(params[:id])
|
410
|
-
|
411
|
-
respond_to do |format|
|
412
|
-
format.html # show.html.erb
|
413
|
-
format.xml { render :xml => @post }
|
414
|
-
end
|
415
|
-
end
|
416
|
-
@@@
|
417
|
-
|
418
|
-
The `show` action uses `Post.find` to search for a single record in the database by its id value. After finding the record, Rails displays it by using `show.html.erb`:
|
419
|
-
|
420
|
-
@@@ html
|
421
|
-
<p class="notice"><%= notice %></p>
|
422
|
-
|
423
|
-
<p>
|
424
|
-
<b>Name:</b>
|
425
|
-
<%= @post.name %>
|
426
|
-
</p>
|
427
|
-
|
428
|
-
<p>
|
429
|
-
<b>Title:</b>
|
430
|
-
<%= @post.title %>
|
431
|
-
</p>
|
432
|
-
|
433
|
-
<p>
|
434
|
-
<b>Content:</b>
|
435
|
-
<%= @post.content %>
|
436
|
-
</p>
|
437
|
-
|
438
|
-
<%= link_to 'Edit', edit_post_path(@post) %> |
|
439
|
-
<%= link_to 'Back', posts_path %>
|
440
|
-
@@@
|
441
|
-
|
442
|
-
### Editing Posts
|
443
|
-
|
444
|
-
Like creating a new post, editing a post is a two-part process. The first step is a request to `edit_post_path(@post)` with a particular post. This calls the `edit` action in the controller:
|
445
|
-
|
446
|
-
@@@ ruby
|
447
|
-
def edit
|
448
|
-
@post = Post.find(params[:id])
|
449
|
-
end
|
450
|
-
@@@
|
451
|
-
|
452
|
-
After finding the requested post, Rails uses the `edit.html.erb` view to display it:
|
453
|
-
|
454
|
-
@@@ html
|
455
|
-
<h1>Editing post</h1>
|
456
|
-
|
457
|
-
<%= render 'form' %>
|
458
|
-
|
459
|
-
<%= link_to 'Show', @post %> |
|
460
|
-
<%= link_to 'Back', posts_path %>
|
461
|
-
@@@
|
462
|
-
|
463
|
-
Again, as with the `new` action, the `edit` action is using the `form` partial, this time however, the form will do a PUT action to the PostsController and the submit button will display “Update Post”.
|
464
|
-
|
465
|
-
Submitting the form created by this view will invoke the `update` action within the controller:
|
466
|
-
|
467
|
-
@@@ ruby
|
468
|
-
def update
|
469
|
-
@post = Post.find(params[:id])
|
470
|
-
|
471
|
-
respond_to do |format|
|
472
|
-
if @post.update_attributes(params[:post])
|
473
|
-
format.html { redirect_to(@post,
|
474
|
-
:notice => 'Post was successfully updated.') }
|
475
|
-
format.xml { head :ok }
|
476
|
-
else
|
477
|
-
format.html { render :action => "edit" }
|
478
|
-
format.xml { render :xml => @post.errors,
|
479
|
-
:status => :unprocessable_entity }
|
480
|
-
end
|
481
|
-
end
|
482
|
-
end
|
483
|
-
@@@
|
484
|
-
|
485
|
-
In the `update` action, Rails first uses the `:id` parameter passed back from the edit view to locate the database record that’s being edited. The `update_attributes` call then takes the rest of the parameters from the request and applies them to this record. If all goes well, the user is redirected to the post’s `show` view. If there are any problems, it’s back to the `edit` view to correct them.
|
486
|
-
|
487
|
-
#### Destroying a Post
|
488
|
-
|
489
|
-
Finally, clicking one of the `destroy` links sends the associated id to the destroy action:
|
490
|
-
|
491
|
-
@@@ ruby
|
492
|
-
def destroy
|
493
|
-
@post = Post.find(params[:id])
|
494
|
-
@post.destroy
|
495
|
-
|
496
|
-
respond_to do |format|
|
497
|
-
format.html { redirect_to(posts_url) }
|
498
|
-
format.xml { head :ok }
|
499
|
-
end
|
500
|
-
end
|
501
|
-
@@@
|
502
|
-
|
503
|
-
The `destroy` method of an Active Record model instance removes the corresponding record from the database. After that’s done, there isn’t any record to display, so Rails redirects the user’s browser to the index view for the model.
|