development: adapter: sqlite3 database: db/development.sqlite3 + pool: 5 timeout: 5000
From a4ff4fd2c38175b38af928da2b5cae2b6bb19da7 Mon Sep 17 00:00:00 2001
From: Mike Gunderloy
+More information: +
++ + | +This Guide is based on Rails 2.3. Some of the code shown here will not work in older versions of Rails. | +
It is highly recommended that you familiarize yourself with Ruby before diving into Rails. You will find it much easier to follow what’s going on with a Rails application if you understand basic Ruby syntax. Rails isn’t going to magically revolutionize the way you write web applications if you have no experience with the language it uses. There are some good free resources on the net for learning Ruby, including:
It is highly recommended that you familiarize yourself with Ruby before diving into Rails. You will find it much easier to follow what’s going on with a Rails application if you understand basic Ruby syntax. Rails isn’t going to magically revolutionize the way you write web applications if you have no experience with the language it uses. There are some good free resources on the internet for learning Ruby, including:
@@ -208,7 +219,7 @@ A working installation of SQLite (preferred
Rails is a web development framework written in the Ruby language. It is designed to make programming web applications easier by making several 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. Longtime Rails developers also report that it makes web application development more fun.
Rails is opinionated software. That is, it assumes that there is a best way to do things, and it’s designed to encourage that best way - and in some cases 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.
Rails is opinionated software. That is, it assumes that there is a best way to do things, and it’s designed to encourage that best 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.
The Rails philosophy includes several guiding principles:
For example, to a Rails application a request such as this:
DELETE /photos/17
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 makes it even more natural by using conventions to shield you from some of the RESTful complexities.
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 makes it even more natural by using conventions to shield you from some of the RESTful complexities and browser quirks.
If you’d like more details on REST as an architectural style, these resources are more approachable than Fielding’s thesis:
$ rails blog -d postgresql
+ + | +You can see all of the switches that the Rails application builder accepts by running rails -h. | +
After you create the blog application, switch to its folder to continue work directly in that application:
development: adapter: sqlite3 database: db/development.sqlite3 + pool: 5 timeout: 5000
If you don’t have any database set up, SQLite is the easiest to get installed. If you’re on OS X 10.5 or greater on a Mac, you already have it. Otherwise, you can install it using RubyGems:
If you’re not running OS X 10.5 or greater, you’ll need to install the SQLite gem. Similar to installing Rails you just need to run:
Change the username and password in the development section as appropriate.
$ rake db:create
++ + | +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 rake -T. | +
$ script/server
This will fire up the lightweight Webrick web server by default. To see your application in action, open a browser window and navigate to http://localhost:3000. You should see Rails' default information page:
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:
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. To view the page you just created, navigate to http://localhost:3000/home/index.
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. To view the page you just created, navigate to http://localhost:3000/home/index.
You’d probably like to replace the "Welcome Aboard" page with your own application’s home page. The first step to doing this is to delete the default page from your application:
The scaffold generator will build 13 files in your application, along with some folders, and edit one more. Here’s a quick overview of what it creates:
The scaffold generator will build 14 files in your application, along with some folders, and edit one more. Here’s a quick overview of what it creates:
The Post model |
||
db/migrate/20081013124235_create_posts.rb |
+db/migrate/20090113124235_create_posts.rb |
Migration to create the posts table in your database (your name will include a different timestamp) |
test/unit/post_test.rb |
Unit testing harness for the posts model |
|
test/unit/helpers/posts_helper_test.rb |
+Unit testing harness for the posts helper |
+
One of the products of the script/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.
If you look in the db/migrate/20081013124235_create_posts.rb file (remember, yours will have a slightly different name), here’s what you’ll find:
If you look in the db/migrate/20090113124235_create_posts.rb file (remember, yours will have a slightly different name), here’s what you’ll find:
$ rake db:create -$ rake db:migrate
$ rake db:migrate
+Remember, you can’t run migrations before running rake db:create to create your database, as we covered earlier.
@@ -842,7 +875,7 @@ title: nil, | -Unlike the development web server, the console does not automatically load your code afresh for each line. If you make changes, type reload! at the console prompt to load them. | +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. |
At this point, it’s worth looking at some of the tools that Rails provides to eliminate duplication in your code. In particular, you can use partials to clean up duplication in views and filters to help with duplication in controllers.
As you saw earlier, the scaffold-generated views for the new and edit actions are largely identical. You can pull the shared code out into a partial template. This requires editing the new and edit views, and adding a new template. The new _form.html.erb template should be saved in the same app/views/posts folder as the files from which it is being extracted:
As you saw earlier, the scaffold-generated views for the new and edit actions are largely identical. You can pull the shared code out into a partial template. This requires editing the new and edit views, and adding a new template. The new _form.html.erb template should be saved in the same app/views/posts folder as the files from which it is being extracted. Note that the name of this file begins with an underscore; that’s the Rails naming convention for partial templates.
new.html.erb:
Rails runs before filters before any action in the controller. You can use the :only clause to limit a before filter to only certain actions, or an :except clause to specifically skip a before filter for certain actions. Rails also allows you to define after filters that run after processing an action, as well as around filters that surround the processing of actions. Filters can also be defined in external classes to make it easy to share them between controllers.
For more information on filters, see the Action Controller Basics guide.
For more information on filters, see the Action Controller Basics guide.
-+db/migrate/20081013214407_create_comments.rb - The migration ++db/migrate/20091013214407_create_comments.rb - The migration
$ rake db:migrate
Rails is smart enough to only execute the migrations that have not already been run against this particular database.
Rails is smart enough to only execute the migrations that have not already been run against the current database.
Active Record associations let you easily declare the relationship between two models. In the case of comments and posts, you could write out the relationships this way:
Routes are entries in the config/routes.rb file that tell Rails how to match incoming HTTP requests to controller actions. Open up that file and find the existing line referring to posts. Then edit it as follows:
Routes are entries in the config/routes.rb file that tell Rails how to match incoming HTTP requests to controller actions. Open up that file and find the existing line referring to posts (it will be right at the top of the file). Then edit it as follows:
map.resources :posts do |post| - post.resources :comments -end
map.resources :posts, :has_many => :comments
This creates comments as a nested resource within posts. This is another part of capturing the hierarchical relationship that exists between posts and comments.
+ + | +You may want to use javascript to dynamically add additional tags on a single form. For an example of this and other advanced techniques, see the nested model sample application. | +
Now that you’ve seen your first Rails application, you should feel free to update it and experiment on your own. But you don’t have to do everything without help. As you need assistance getting up and running with Rails, feel free to consult these support resources:
-November 3, 2008: Formatting patch from Dave Rothlisberger -
--November 1, 2008: First approved version by Mike Gunderloy -
--October 16, 2008: Revised based on feedback from Pratik Naik by Mike Gunderloy (not yet approved for publication) -
--October 13, 2008: First complete draft by Mike Gunderloy (not yet approved for publication) -
--October 12, 2008: More detail, rearrangement, editing by Mike Gunderloy (not yet approved for publication) -
--September 8, 2008: initial version by James Miller (not yet approved for publication) -
-* +* November 3, 2008: Formatting patch from Dave Rothlisberger +* November 1, 2008: First approved version by Mike Gunderloy +* October 16, 2008: Revised based on feedback from Pratik Naik by Mike Gunderloy (not yet approved for publication) +* October 13, 2008: First complete draft by Mike Gunderloy (not yet approved for publication) +* October 12, 2008: More detail, rearrangement, editing by Mike Gunderloy (not yet approved for publication) +* September 8, 2008: initial version by James Miller (not yet approved for publication)