Rails-based website for local business
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

256 lines
8.9 KiB

  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.find(: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.com/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. | |-- controllers
  114. | |-- helpers
  115. | |-- mailers
  116. | |-- models
  117. | `-- views
  118. | `-- layouts
  119. |-- config
  120. | |-- environments
  121. | |-- initializers
  122. | `-- locales
  123. |-- db
  124. |-- doc
  125. |-- lib
  126. | `-- tasks
  127. |-- log
  128. |-- public
  129. | |-- images
  130. | |-- javascripts
  131. | `-- stylesheets
  132. |-- script
  133. |-- test
  134. | |-- fixtures
  135. | |-- functional
  136. | |-- integration
  137. | |-- performance
  138. | `-- unit
  139. |-- tmp
  140. | |-- cache
  141. | |-- pids
  142. | |-- sessions
  143. | `-- sockets
  144. `-- vendor
  145. `-- plugins
  146. app
  147. Holds all the code that's specific to this particular application.
  148. app/controllers
  149. Holds controllers that should be named like weblogs_controller.rb for
  150. automated URL mapping. All controllers should descend from
  151. ApplicationController which itself descends from ActionController::Base.
  152. app/models
  153. Holds models that should be named like post.rb. Models descend from
  154. ActiveRecord::Base by default.
  155. app/views
  156. Holds the template files for the view that should be named like
  157. weblogs/index.html.erb for the WeblogsController#index action. All views use
  158. eRuby syntax by default.
  159. app/views/layouts
  160. Holds the template files for layouts to be used with views. This models the
  161. common header/footer method of wrapping views. In your views, define a layout
  162. using the <tt>layout :default</tt> and create a file named default.html.erb.
  163. Inside default.html.erb, call <% yield %> to render the view using this
  164. layout.
  165. app/helpers
  166. Holds view helpers that should be named like weblogs_helper.rb. These are
  167. generated for you automatically when using generators for controllers.
  168. Helpers can be used to wrap functionality for your views into methods.
  169. config
  170. Configuration files for the Rails environment, the routing map, the database,
  171. and other dependencies.
  172. db
  173. Contains the database schema in schema.rb. db/migrate contains all the
  174. sequence of Migrations for your schema.
  175. doc
  176. This directory is where your application documentation will be stored when
  177. generated using <tt>rake doc:app</tt>
  178. lib
  179. Application specific libraries. Basically, any kind of custom code that
  180. doesn't belong under controllers, models, or helpers. This directory is in
  181. the load path.
  182. public
  183. The directory available for the web server. Contains subdirectories for
  184. images, stylesheets, and javascripts. Also contains the dispatchers and the
  185. default HTML files. This should be set as the DOCUMENT_ROOT of your web
  186. server.
  187. script
  188. Helper scripts for automation and generation.
  189. test
  190. Unit and functional tests along with fixtures. When using the rails generate
  191. command, template test files will be generated for you and placed in this
  192. directory.
  193. vendor
  194. External libraries that the application depends on. Also includes the plugins
  195. subdirectory. If the app has frozen rails, those gems also go here, under
  196. vendor/rails/. This directory is in the load path.