root/websites/bakagaiku/README

Revision 3209, 7.2 kB (checked in by hsbt, 4 years ago)

lang/ruby/bakagaiku: import from my repos.

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