| 1 |
to start, if you're on live, copy copy/live.yml over config/database.yml |
|---|
| 2 |
|
|---|
| 3 |
== Welcome to Rails |
|---|
| 4 |
|
|---|
| 5 |
Rails is a web-application and persistence framework that includes everything |
|---|
| 6 |
needed to create database-backed web-applications according to the |
|---|
| 7 |
Model-View-Control pattern of separation. This pattern splits the view (also |
|---|
| 8 |
called the presentation) into "dumb" templates that are primarily responsible |
|---|
| 9 |
for inserting pre-built data in between HTML tags. The model contains the |
|---|
| 10 |
"smart" domain objects (such as Account, Product, Person, Post) that holds all |
|---|
| 11 |
the business logic and knows how to persist themselves to a database. The |
|---|
| 12 |
controller handles the incoming requests (such as Save New Account, Update |
|---|
| 13 |
Product, Show Post) by manipulating the model and directing data to the view. |
|---|
| 14 |
|
|---|
| 15 |
In Rails, the model is handled by what's called an object-relational mapping |
|---|
| 16 |
layer entitled Active Record. This layer allows you to present the data from |
|---|
| 17 |
database rows as objects and embellish these data objects with business logic |
|---|
| 18 |
methods. You can read more about Active Record in |
|---|
| 19 |
link:files/vendor/rails/activerecord/README.html. |
|---|
| 20 |
|
|---|
| 21 |
The controller and view are handled by the Action Pack, which handles both |
|---|
| 22 |
layers by its two parts: Action View and Action Controller. These two layers |
|---|
| 23 |
are bundled in a single package due to their heavy interdependence. This is |
|---|
| 24 |
unlike the relationship between the Active Record and Action Pack that is much |
|---|
| 25 |
more separate. Each of these packages can be used independently outside of |
|---|
| 26 |
Rails. You can read more about Action Pack in |
|---|
| 27 |
link:files/vendor/rails/actionpack/README.html. |
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
== Getting started |
|---|
| 31 |
|
|---|
| 32 |
1. Start the web server: <tt>ruby script/server</tt> (run with --help for options) |
|---|
| 33 |
2. Go to http://localhost:3000/ and get "Welcome aboard: You’re riding the Rails!" |
|---|
| 34 |
3. Follow the guidelines to start developing your application |
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
== Web servers |
|---|
| 38 |
|
|---|
| 39 |
Rails uses the built-in web server in Ruby called WEBrick by default, so you don't |
|---|
| 40 |
have to install or configure anything to play around. |
|---|
| 41 |
|
|---|
| 42 |
If you have lighttpd installed, though, it'll be used instead when running script/server. |
|---|
| 43 |
It's considerably faster than WEBrick and suited for production use, but requires additional |
|---|
| 44 |
installation and currently only works well on OS X/Unix (Windows users are encouraged |
|---|
| 45 |
to start with WEBrick). We recommend version 1.4.11 and higher. You can download it from |
|---|
| 46 |
http://www.lighttpd.net. |
|---|
| 47 |
|
|---|
| 48 |
If you want something that's halfway between WEBrick and lighttpd, we heartily recommend |
|---|
| 49 |
Mongrel. It's a Ruby-based web server with a C-component (so it requires compilation) that |
|---|
| 50 |
also works very well with Windows. See more at http://mongrel.rubyforge.org/. |
|---|
| 51 |
|
|---|
| 52 |
But of course its also possible to run Rails with the premiere open source web server Apache. |
|---|
| 53 |
To get decent performance, though, you'll need to install FastCGI. For Apache 1.3, you want |
|---|
| 54 |
to use mod_fastcgi. For Apache 2.0+, you want to use mod_fcgid. |
|---|
| 55 |
|
|---|
| 56 |
See http://wiki.rubyonrails.com/rails/pages/FastCGI for more information on FastCGI. |
|---|
| 57 |
|
|---|
| 58 |
== Example for Apache conf |
|---|
| 59 |
|
|---|
| 60 |
<VirtualHost *:80> |
|---|
| 61 |
ServerName rails |
|---|
| 62 |
DocumentRoot /path/application/public/ |
|---|
| 63 |
ErrorLog /path/application/log/server.log |
|---|
| 64 |
|
|---|
| 65 |
<Directory /path/application/public/> |
|---|
| 66 |
Options ExecCGI FollowSymLinks |
|---|
| 67 |
AllowOverride all |
|---|
| 68 |
Allow from all |
|---|
| 69 |
Order allow,deny |
|---|
| 70 |
</Directory> |
|---|
| 71 |
</VirtualHost> |
|---|
| 72 |
|
|---|
| 73 |
NOTE: Be sure that CGIs can be executed in that directory as well. So ExecCGI |
|---|
| 74 |
should be on and ".cgi" should respond. All requests from 127.0.0.1 go |
|---|
| 75 |
through CGI, so no Apache restart is necessary for changes. All other requests |
|---|
| 76 |
go through FCGI (or mod_ruby), which requires a restart to show changes. |
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
== Debugging Rails |
|---|
| 80 |
|
|---|
| 81 |
Have "tail -f" commands running on both the server.log, production.log, and |
|---|
| 82 |
test.log files. Rails will automatically display debugging and runtime |
|---|
| 83 |
information to these files. Debugging info will also be shown in the browser |
|---|
| 84 |
on requests from 127.0.0.1. |
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
== Breakpoints |
|---|
| 88 |
|
|---|
| 89 |
Breakpoint support is available through the script/breakpointer client. This |
|---|
| 90 |
means that you can break out of execution at any point in the code, investigate |
|---|
| 91 |
and change the model, AND then resume execution! Example: |
|---|
| 92 |
|
|---|
| 93 |
class WeblogController < ActionController::Base |
|---|
| 94 |
def index |
|---|
| 95 |
@posts = Post.find_all |
|---|
| 96 |
breakpoint "Breaking out from the list" |
|---|
| 97 |
end |
|---|
| 98 |
end |
|---|
| 99 |
|
|---|
| 100 |
So the controller will accept the action, run the first line, then present you |
|---|
| 101 |
with a IRB prompt in the breakpointer window. Here you can do things like: |
|---|
| 102 |
|
|---|
| 103 |
Executing breakpoint "Breaking out from the list" at .../webrick_server.rb:16 in 'breakpoint' |
|---|
| 104 |
|
|---|
| 105 |
>> @posts.inspect |
|---|
| 106 |
=> "[#<Post:0x14a6be8 @attributes={\"title\"=>nil, \"body\"=>nil, \"id\"=>\"1\"}>, |
|---|
| 107 |
#<Post:0x14a6620 @attributes={\"title\"=>\"Rails you know!\", \"body\"=>\"Only ten..\", \"id\"=>\"2\"}>]" |
|---|
| 108 |
>> @posts.first.title = "hello from a breakpoint" |
|---|
| 109 |
=> "hello from a breakpoint" |
|---|
| 110 |
|
|---|
| 111 |
...and even better is that you can examine how your runtime objects actually work: |
|---|
| 112 |
|
|---|
| 113 |
>> f = @posts.first |
|---|
| 114 |
=> #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}> |
|---|
| 115 |
>> f. |
|---|
| 116 |
Display all 152 possibilities? (y or n) |
|---|
| 117 |
|
|---|
| 118 |
Finally, when you're ready to resume execution, you press CTRL-D |
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
== Console |
|---|
| 122 |
|
|---|
| 123 |
You can interact with the domain model by starting the console through script/console. |
|---|
| 124 |
Here you'll have all parts of the application configured, just like it is when the |
|---|
| 125 |
application is running. You can inspect domain models, change values, and save to the |
|---|
| 126 |
database. Starting the script without arguments will launch it in the development environment. |
|---|
| 127 |
Passing an argument will specify a different environment, like <tt>script/console production</tt>. |
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
== Description of contents |
|---|
| 131 |
|
|---|
| 132 |
app |
|---|
| 133 |
Holds all the code that's specific to this particular application. |
|---|
| 134 |
|
|---|
| 135 |
app/controllers |
|---|
| 136 |
Holds controllers that should be named like weblog_controller.rb for |
|---|
| 137 |
automated URL mapping. All controllers should descend from |
|---|
| 138 |
ActionController::Base. |
|---|
| 139 |
|
|---|
| 140 |
app/models |
|---|
| 141 |
Holds models that should be named like post.rb. |
|---|
| 142 |
Most models will descend from ActiveRecord::Base. |
|---|
| 143 |
|
|---|
| 144 |
app/views |
|---|
| 145 |
Holds the template files for the view that should be named like |
|---|
| 146 |
weblog/index.rhtml for the WeblogController#index action. All views use eRuby |
|---|
| 147 |
syntax. This directory can also be used to keep stylesheets, images, and so on |
|---|
| 148 |
that can be symlinked to public. |
|---|
| 149 |
|
|---|
| 150 |
app/helpers |
|---|
| 151 |
Holds view helpers that should be named like weblog_helper.rb. |
|---|
| 152 |
|
|---|
| 153 |
app/apis |
|---|
| 154 |
Holds API classes for web services. |
|---|
| 155 |
|
|---|
| 156 |
config |
|---|
| 157 |
Configuration files for the Rails environment, the routing map, the database, and other dependencies. |
|---|
| 158 |
|
|---|
| 159 |
components |
|---|
| 160 |
Self-contained mini-applications that can bundle together controllers, models, and views. |
|---|
| 161 |
|
|---|
| 162 |
db |
|---|
| 163 |
Contains the database schema in schema.rb. db/migrate contains all |
|---|
| 164 |
the sequence of Migrations for your schema. |
|---|
| 165 |
|
|---|
| 166 |
lib |
|---|
| 167 |
Application specific libraries. Basically, any kind of custom code that doesn't |
|---|
| 168 |
belong under controllers, models, or helpers. This directory is in the load path. |
|---|
| 169 |
|
|---|
| 170 |
public |
|---|
| 171 |
The directory available for the web server. Contains subdirectories for images, stylesheets, |
|---|
| 172 |
and javascripts. Also contains the dispatchers and the default HTML files. |
|---|
| 173 |
|
|---|
| 174 |
script |
|---|
| 175 |
Helper scripts for automation and generation. |
|---|
| 176 |
|
|---|
| 177 |
test |
|---|
| 178 |
Unit and functional tests along with fixtures. |
|---|
| 179 |
|
|---|
| 180 |
vendor |
|---|
| 181 |
External libraries that the application depends on. Also includes the plugins subdirectory. |
|---|
| 182 |
This directory is in the load path. |
|---|