README.rdoc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. == Welcome to Rails
  2. Rails is a web-application framework that includes everything needed to create
  3. database-backed web applications according to the Model-View-Control pattern.
  4. This pattern splits the view (also called the presentation) into "dumb"
  5. templates that are primarily responsible for inserting pre-built data in between
  6. HTML tags. The model contains the "smart" domain objects (such as Account,
  7. Product, Person, Post) that holds all the business logic and knows how to
  8. persist themselves to a database. The controller handles the incoming requests
  9. (such as Save New Account, Update Product, Show Post) by manipulating the model
  10. and directing data to the view.
  11. In Rails, the model is handled by what's called an 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/rails/activerecord/README.html.
  16. The controller and view are 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/rails/actionpack/README.html.
  23. == Getting Started
  24. 1. At the command prompt, create a new Rails application:
  25. <tt>rails new myapp</tt> (where <tt>myapp</tt> is the application name)
  26. 2. Change directory to <tt>myapp</tt> and start the web server:
  27. <tt>cd myapp; rails server</tt> (run with --help for options)
  28. 3. Go to http://localhost:3000/ and you'll see:
  29. "Welcome aboard: You're riding Ruby on Rails!"
  30. 4. Follow the guidelines to start developing your application. You can find
  31. the following resources handy:
  32. * The Getting Started Guide: http://guides.rubyonrails.org/getting_started.html
  33. * Ruby on Rails Tutorial Book: http://www.railstutorial.org/
  34. == Debugging Rails
  35. Sometimes your application goes wrong. Fortunately there are a lot of tools that
  36. will help you debug it and get it back on the rails.
  37. First area to check is the application log files. Have "tail -f" commands
  38. running on the server.log and development.log. Rails will automatically display
  39. debugging and runtime information to these files. Debugging info will also be
  40. shown in the browser on requests from 127.0.0.1.
  41. You can also log your own messages directly into the log file from your code
  42. using the Ruby logger class from inside your controllers. Example:
  43. class WeblogController < ActionController::Base
  44. def destroy
  45. @weblog = Weblog.find(params[:id])
  46. @weblog.destroy
  47. logger.info("#{Time.now} Destroyed Weblog ID ##{@weblog.id}!")
  48. end
  49. end
  50. The result will be a message in your log file along the lines of:
  51. Mon Oct 08 14:22:29 +1000 2007 Destroyed Weblog ID #1!
  52. More information on how to use the logger is at http://www.ruby-doc.org/core/
  53. Also, Ruby documentation can be found at http://www.ruby-lang.org/. There are
  54. several books available online as well:
  55. * Programming Ruby: http://www.ruby-doc.org/docs/ProgrammingRuby/ (Pickaxe)
  56. * Learn to Program: http://pine.fm/LearnToProgram/ (a beginners guide)
  57. These two books will bring you up to speed on the Ruby language and also on
  58. programming in general.
  59. == Debugger
  60. Debugger support is available through the debugger command when you start your
  61. Mongrel or WEBrick server with --debugger. This means that you can break out of
  62. execution at any point in the code, investigate and change the model, and then,
  63. resume execution! You need to install ruby-debug to run the server in debugging
  64. mode. With gems, use <tt>sudo gem install ruby-debug</tt>. Example:
  65. class WeblogController < ActionController::Base
  66. def index
  67. @posts = Post.all
  68. debugger
  69. end
  70. end
  71. So the controller will accept the action, run the first line, then present you
  72. with a IRB prompt in the server window. Here you can do things like:
  73. >> @posts.inspect
  74. => "[#<Post:0x14a6be8
  75. @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>,
  76. #<Post:0x14a6620
  77. @attributes={"title"=>"Rails", "body"=>"Only ten..", "id"=>"2"}>]"
  78. >> @posts.first.title = "hello from a debugger"
  79. => "hello from a debugger"
  80. ...and even better, you can examine how your runtime objects actually work:
  81. >> f = @posts.first
  82. => #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>
  83. >> f.
  84. Display all 152 possibilities? (y or n)
  85. Finally, when you're ready to resume execution, you can enter "cont".
  86. == Console
  87. The console is a Ruby shell, which allows you to interact with your
  88. application's domain model. Here you'll have all parts of the application
  89. configured, just like it is when the application is running. You can inspect
  90. domain models, change values, and save to the database. Starting the script
  91. without arguments will launch it in the development environment.
  92. To start the console, run <tt>rails console</tt> from the application
  93. directory.
  94. Options:
  95. * Passing the <tt>-s, --sandbox</tt> argument will rollback any modifications
  96. made to the database.
  97. * Passing an environment name as an argument will load the corresponding
  98. environment. Example: <tt>rails console production</tt>.
  99. To reload your controllers and models after launching the console run
  100. <tt>reload!</tt>
  101. More information about irb can be found at:
  102. link:http://www.rubycentral.org/pickaxe/irb.html
  103. == dbconsole
  104. You can go to the command line of your database directly through <tt>rails
  105. dbconsole</tt>. You would be connected to the database with the credentials
  106. defined in database.yml. Starting the script without arguments will connect you
  107. to the development database. Passing an argument will connect you to a different
  108. database, like <tt>rails dbconsole production</tt>. Currently works for MySQL,
  109. PostgreSQL and SQLite 3.
  110. == Description of Contents
  111. The default directory structure of a generated Ruby on Rails application:
  112. |-- app
  113. | |-- assets
  114. | |-- images
  115. | |-- javascripts
  116. | `-- stylesheets
  117. | |-- controllers
  118. | |-- helpers
  119. | |-- mailers
  120. | |-- models
  121. | `-- views
  122. | `-- layouts
  123. |-- config
  124. | |-- environments
  125. | |-- initializers
  126. | `-- locales
  127. |-- db
  128. |-- doc
  129. |-- lib
  130. | `-- tasks
  131. |-- log
  132. |-- public
  133. |-- script
  134. |-- test
  135. | |-- fixtures
  136. | |-- functional
  137. | |-- integration
  138. | |-- performance
  139. | `-- unit
  140. |-- tmp
  141. | |-- cache
  142. | |-- pids
  143. | |-- sessions
  144. | `-- sockets
  145. `-- vendor
  146. |-- assets
  147. `-- stylesheets
  148. `-- plugins
  149. app
  150. Holds all the code that's specific to this particular application.
  151. app/assets
  152. Contains subdirectories for images, stylesheets, and JavaScript files.
  153. app/controllers
  154. Holds controllers that should be named like weblogs_controller.rb for
  155. automated URL mapping. All controllers should descend from
  156. ApplicationController which itself descends from ActionController::Base.
  157. app/models
  158. Holds models that should be named like post.rb. Models descend from
  159. ActiveRecord::Base by default.
  160. app/views
  161. Holds the template files for the view that should be named like
  162. weblogs/index.html.erb for the WeblogsController#index action. All views use
  163. eRuby syntax by default.
  164. app/views/layouts
  165. Holds the template files for layouts to be used with views. This models the
  166. common header/footer method of wrapping views. In your views, define a layout
  167. using the <tt>layout :default</tt> and create a file named default.html.erb.
  168. Inside default.html.erb, call <% yield %> to render the view using this
  169. layout.
  170. app/helpers
  171. Holds view helpers that should be named like weblogs_helper.rb. These are
  172. generated for you automatically when using generators for controllers.
  173. Helpers can be used to wrap functionality for your views into methods.
  174. config
  175. Configuration files for the Rails environment, the routing map, the database,
  176. and other dependencies.
  177. db
  178. Contains the database schema in schema.rb. db/migrate contains all the
  179. sequence of Migrations for your schema.
  180. doc
  181. This directory is where your application documentation will be stored when
  182. generated using <tt>rake doc:app</tt>
  183. lib
  184. Application specific libraries. Basically, any kind of custom code that
  185. doesn't belong under controllers, models, or helpers. This directory is in
  186. the load path.
  187. public
  188. The directory available for the web server. Also contains the dispatchers and the
  189. default HTML files. This should be set as the DOCUMENT_ROOT of your web
  190. server.
  191. script
  192. Helper scripts for automation and generation.
  193. test
  194. Unit and functional tests along with fixtures. When using the rails generate
  195. command, template test files will be generated for you and placed in this
  196. directory.
  197. vendor
  198. External libraries that the application depends on. Also includes the plugins
  199. subdirectory. If the app has frozen rails, those gems also go here, under
  200. vendor/rails/. This directory is in the load path.