README 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. == Welcome to Rails
  2. Rails is a web-application and persistance framework that includes everything
  3. needed to create database-backed web-applications according to the
  4. Model-View-Control pattern of separation. This pattern splits the view (also
  5. called the presentation) into "dumb" templates that are primarily responsible
  6. for inserting pre-build data in between HTML tags. The model contains the
  7. "smart" domain objects (such as Account, Product, Person, Post) that holds all
  8. the business logic and knows how to persist themselves to a database. The
  9. controller handles the incoming requests (such as Save New Account, Update
  10. Product, Show Post) by manipulating the model and directing data to the view.
  11. In Rails, the model is handled by what's called a object-relational mapping
  12. layer entitled Active Record. This layer allows you to present the data from
  13. database rows as objects and embellish these data objects with business logic
  14. methods. You can read more about Active Record in
  15. link:files/vendor/activerecord/README.html.
  16. The controller and view is handled by the Action Pack, which handles both
  17. layers by its two parts: Action View and Action Controller. These two layers
  18. are bundled in a single package due to their heavy interdependence. This is
  19. unlike the relationship between the Active Record and Action Pack that is much
  20. more separate. Each of these packages can be used independently outside of
  21. Rails. You can read more about Action Pack in
  22. link:files/vendor/actionpack/README.html.
  23. == Requirements
  24. * Database and driver (MySQL, PostgreSQL, or SQLite)
  25. * Rake[http://rake.rubyforge.org] for running tests and the generating documentation
  26. == Optionals
  27. * Apache 1.3.x or 2.x or lighttpd 1.3.11+ (or any FastCGI-capable webserver with a
  28. mod_rewrite-like module)
  29. * FastCGI (or mod_ruby) for better performance on Apache
  30. == Getting started
  31. 1. Run the WEBrick servlet: <tt>ruby script/server</tt>
  32. (run with --help for options)
  33. 2. Go to http://localhost:3000/ and get "Congratulations, you've put Ruby on Rails!"
  34. 3. Follow the guidelines on the "Congratulations, you've put Ruby on Rails!" screen
  35. == Example for Apache conf
  36. <VirtualHost *:80>
  37. ServerName rails
  38. DocumentRoot /path/application/public/
  39. ErrorLog /path/application/log/server.log
  40. <Directory /path/application/public/>
  41. Options ExecCGI FollowSymLinks
  42. AllowOverride all
  43. Allow from all
  44. Order allow,deny
  45. </Directory>
  46. </VirtualHost>
  47. NOTE: Be sure that CGIs can be executed in that directory as well. So ExecCGI
  48. should be on and ".cgi" should respond. All requests from 127.0.0.1 goes
  49. through CGI, so no Apache restart is necessary for changes. All other requests
  50. goes through FCGI (or mod_ruby) that requires restart to show changes.
  51. == Example for lighttpd conf (with FastCGI)
  52. server.port = 8080
  53. server.bind = "127.0.0.1"
  54. # server.event-handler = "freebsd-kqueue" # needed on OS X
  55. server.modules = ( "mod_rewrite", "mod_fastcgi" )
  56. url.rewrite = ( "^/$" => "index.html", "^([^.]+)$" => "$1.html" )
  57. server.error-handler-404 = "/dispatch.fcgi"
  58. server.document-root = "/path/application/public"
  59. server.errorlog = "/path/application/log/server.log"
  60. fastcgi.server = ( ".fcgi" =>
  61. ( "localhost" =>
  62. (
  63. "min-procs" => 1,
  64. "max-procs" => 5,
  65. "socket" => "/tmp/application.fcgi.socket",
  66. "bin-path" => "/path/application/public/dispatch.fcgi",
  67. "bin-environment" => ( "RAILS_ENV" => "development" )
  68. )
  69. )
  70. )
  71. == Debugging Rails
  72. Have "tail -f" commands running on both the server.log, production.log, and
  73. test.log files. Rails will automatically display debugging and runtime
  74. information to these files. Debugging info will also be shown in the browser
  75. on requests from 127.0.0.1.
  76. == Breakpoints
  77. Breakpoint support is available through the script/breakpointer client. This
  78. means that you can break out of execution at any point in the code, investigate
  79. and change the model, AND then resume execution! Example:
  80. class WeblogController < ActionController::Base
  81. def index
  82. @posts = Post.find_all
  83. breakpoint "Breaking out from the list"
  84. end
  85. end
  86. So the controller will accept the action, run the first line, then present you
  87. with a IRB prompt in the breakpointer window. Here you can do things like:
  88. Executing breakpoint "Breaking out from the list" at .../webrick_server.rb:16 in 'breakpoint'
  89. >> @posts.inspect
  90. => "[#<Post:0x14a6be8 @attributes={\"title\"=>nil, \"body\"=>nil, \"id\"=>\"1\"}>,
  91. #<Post:0x14a6620 @attributes={\"title\"=>\"Rails you know!\", \"body\"=>\"Only ten..\", \"id\"=>\"2\"}>]"
  92. >> @posts.first.title = "hello from a breakpoint"
  93. => "hello from a breakpoint"
  94. ...and even better is that you can examine how your runtime objects actually work:
  95. >> f = @posts.first
  96. => #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>
  97. >> f.
  98. Display all 152 possibilities? (y or n)
  99. Finally, when you're ready to resume execution, you press CTRL-D
  100. == Console
  101. You can interact with the domain model by starting the console through script/console.
  102. Here you'll have all parts of the application configured, just like it is when the
  103. application is running. You can inspect domain models, change values, and save to the
  104. database. Start the script without arguments will launch it in the development environment.
  105. Passing an argument will specify a different environment, like <tt>console production</tt>.
  106. == Description of contents
  107. app
  108. Holds all the code that's specific to this particular application.
  109. app/controllers
  110. Holds controllers that should be named like weblog_controller.rb for
  111. automated URL mapping. All controllers should descend from
  112. ActionController::Base.
  113. app/models
  114. Holds models that should be named like post.rb.
  115. Most models will descent from ActiveRecord::Base.
  116. app/views
  117. Holds the template files for the view that should be named like
  118. weblog/index.rhtml for the WeblogController#index action. All views uses eRuby
  119. syntax. This directory can also be used to keep stylesheets, images, and so on
  120. that can be symlinked to public.
  121. app/helpers
  122. Holds view helpers that should be named like weblog_helper.rb.
  123. config
  124. Configuration files for the Rails environment, the routing map, the database, and other dependencies.
  125. components
  126. Self-contained mini-applications that can bundle controllers, models, and views together.
  127. lib
  128. Application specific libraries. Basically, any kind of custom code that doesn't
  129. belong controllers, models, or helpers. This directory is in the load path.
  130. public
  131. The directory available for the web server. Contains sub-directories for images, stylesheets,
  132. and javascripts. Also contains the dispatchers and the default HTML files.
  133. script
  134. Helper scripts for automation and generation.
  135. test
  136. Unit and functional tests along with fixtures.
  137. vendor
  138. External libraries that the application depend on. This directory is in the load path.