Basis of rails-driven app 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
10 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" templates
  5. that are primarily responsible for inserting pre-built data in between HTML tags.
  6. The model contains the "smart" domain objects (such as Account, Product, Person,
  7. Post) that holds all the business logic and knows how to persist themselves to
  8. a database. The controller handles the incoming requests (such as Save New Account,
  9. Update Product, Show Post) by manipulating the model and directing data to the view.
  10. In Rails, the model is handled by what's called an object-relational mapping
  11. layer entitled Active Record. This layer allows you to present the data from
  12. database rows as objects and embellish these data objects with business logic
  13. methods. You can read more about Active Record in
  14. link:files/vendor/rails/activerecord/README.html.
  15. The controller and view are handled by the Action Pack, which handles both
  16. layers by its two parts: Action View and Action Controller. These two layers
  17. are bundled in a single package due to their heavy interdependence. This is
  18. unlike the relationship between the Active Record and Action Pack that is much
  19. more separate. Each of these packages can be used independently outside of
  20. Rails. You can read more about Action Pack in
  21. link:files/vendor/rails/actionpack/README.html.
  22. == Getting Started
  23. 1. At the command prompt, start a new Rails application using the <tt>rails</tt> command
  24. and your application name. Ex: rails myapp
  25. 2. Change directory into myapp and start the web server: <tt>script/server</tt> (run with --help for options)
  26. 3. Go to http://localhost:3000/ and get "Welcome aboard: You're riding the Rails!"
  27. 4. Follow the guidelines to start developing your application
  28. == Web Servers
  29. By default, Rails will try to use Mongrel and lighttpd if they are installed, otherwise
  30. Rails will use WEBrick, the webserver that ships with Ruby. When you run script/server,
  31. Rails will check if Mongrel exists, then lighttpd and finally fall back to WEBrick. This ensures
  32. that you can always get up and running quickly.
  33. Mongrel is a Ruby-based webserver with a C component (which requires compilation) that is
  34. suitable for development and deployment of Rails applications. If you have Ruby Gems installed,
  35. getting up and running with mongrel is as easy as: <tt>gem install mongrel</tt>.
  36. More info at: http://mongrel.rubyforge.org
  37. If Mongrel is not installed, Rails will look for lighttpd. It's considerably faster than
  38. Mongrel and WEBrick and also suited for production use, but requires additional
  39. installation and currently only works well on OS X/Unix (Windows users are encouraged
  40. to start with Mongrel). We recommend version 1.4.11 and higher. You can download it from
  41. http://www.lighttpd.net.
  42. And finally, if neither Mongrel or lighttpd are installed, Rails will use the built-in Ruby
  43. web server, WEBrick. WEBrick is a small Ruby web server suitable for development, but not
  44. for production.
  45. But of course its also possible to run Rails on any platform that supports FCGI.
  46. Apache, LiteSpeed, IIS are just a few. For more information on FCGI,
  47. please visit: http://wiki.rubyonrails.com/rails/pages/FastCGI
  48. == Apache .htaccess example
  49. # General Apache options
  50. AddHandler fastcgi-script .fcgi
  51. AddHandler cgi-script .cgi
  52. Options +FollowSymLinks +ExecCGI
  53. # If you don't want Rails to look in certain directories,
  54. # use the following rewrite rules so that Apache won't rewrite certain requests
  55. #
  56. # Example:
  57. # RewriteCond %{REQUEST_URI} ^/notrails.*
  58. # RewriteRule .* - [L]
  59. # Redirect all requests not available on the filesystem to Rails
  60. # By default the cgi dispatcher is used which is very slow
  61. #
  62. # For better performance replace the dispatcher with the fastcgi one
  63. #
  64. # Example:
  65. # RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
  66. RewriteEngine On
  67. # If your Rails application is accessed via an Alias directive,
  68. # then you MUST also set the RewriteBase in this htaccess file.
  69. #
  70. # Example:
  71. # Alias /myrailsapp /path/to/myrailsapp/public
  72. # RewriteBase /myrailsapp
  73. RewriteRule ^$ index.html [QSA]
  74. RewriteRule ^([^.]+)$ $1.html [QSA]
  75. RewriteCond %{REQUEST_FILENAME} !-f
  76. RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
  77. # In case Rails experiences terminal errors
  78. # Instead of displaying this message you can supply a file here which will be rendered instead
  79. #
  80. # Example:
  81. # ErrorDocument 500 /500.html
  82. ErrorDocument 500 "<h2>Application error</h2>Rails application failed to start properly"
  83. == Debugging Rails
  84. Sometimes your application goes wrong. Fortunately there are a lot of tools that
  85. will help you debug it and get it back on the rails.
  86. First area to check is the application log files. Have "tail -f" commands running
  87. on the server.log and development.log. Rails will automatically display debugging
  88. and runtime information to these files. Debugging info will also be shown in the
  89. browser on requests from 127.0.0.1.
  90. You can also log your own messages directly into the log file from your code using
  91. the Ruby logger class from inside your controllers. Example:
  92. class WeblogController < ActionController::Base
  93. def destroy
  94. @weblog = Weblog.find(params[:id])
  95. @weblog.destroy
  96. logger.info("#{Time.now} Destroyed Weblog ID ##{@weblog.id}!")
  97. end
  98. end
  99. The result will be a message in your log file along the lines of:
  100. Mon Oct 08 14:22:29 +1000 2007 Destroyed Weblog ID #1
  101. More information on how to use the logger is at http://www.ruby-doc.org/core/
  102. Also, Ruby documentation can be found at http://www.ruby-lang.org/ including:
  103. * The Learning Ruby (Pickaxe) Book: http://www.ruby-doc.org/docs/ProgrammingRuby/
  104. * Learn to Program: http://pine.fm/LearnToProgram/ (a beginners guide)
  105. These two online (and free) books will bring you up to speed on the Ruby language
  106. and also on programming in general.
  107. == Debugger
  108. Debugger support is available through the debugger command when you start your Mongrel or
  109. Webrick server with --debugger. This means that you can break out of execution at any point
  110. in the code, investigate and change the model, AND then resume execution!
  111. You need to install ruby-debug to run the server in debugging mode. With gems, use 'gem install ruby-debug'
  112. Example:
  113. class WeblogController < ActionController::Base
  114. def index
  115. @posts = Post.find(:all)
  116. debugger
  117. end
  118. end
  119. So the controller will accept the action, run the first line, then present you
  120. with a IRB prompt in the server window. Here you can do things like:
  121. >> @posts.inspect
  122. => "[#<Post:0x14a6be8 @attributes={\"title\"=>nil, \"body\"=>nil, \"id\"=>\"1\"}>,
  123. #<Post:0x14a6620 @attributes={\"title\"=>\"Rails you know!\", \"body\"=>\"Only ten..\", \"id\"=>\"2\"}>]"
  124. >> @posts.first.title = "hello from a debugger"
  125. => "hello from a debugger"
  126. ...and even better is that you can examine how your runtime objects actually work:
  127. >> f = @posts.first
  128. => #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>
  129. >> f.
  130. Display all 152 possibilities? (y or n)
  131. Finally, when you're ready to resume execution, you enter "cont"
  132. == Console
  133. You can interact with the domain model by starting the console through <tt>script/console</tt>.
  134. Here you'll have all parts of the application configured, just like it is when the
  135. application is running. You can inspect domain models, change values, and save to the
  136. database. Starting the script without arguments will launch it in the development environment.
  137. Passing an argument will specify a different environment, like <tt>script/console production</tt>.
  138. To reload your controllers and models after launching the console run <tt>reload!</tt>
  139. == dbconsole
  140. You can go to the command line of your database directly through <tt>script/dbconsole</tt>.
  141. You would be connected to the database with the credentials defined in database.yml.
  142. Starting the script without arguments will connect you to the development database. Passing an
  143. argument will connect you to a different database, like <tt>script/dbconsole production</tt>.
  144. Currently works for mysql, postgresql and sqlite.
  145. == Description of Contents
  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 ApplicationController
  151. which itself descends from ActionController::Base.
  152. app/models
  153. Holds models that should be named like post.rb.
  154. Most models will descend from ActiveRecord::Base.
  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 eRuby
  158. syntax.
  159. app/views/layouts
  160. Holds the template files for layouts to be used with views. This models the common
  161. header/footer method of wrapping views. In your views, define a layout using the
  162. <tt>layout :default</tt> and create a file named default.html.erb. Inside default.html.erb,
  163. call <% yield %> to render the view using this layout.
  164. app/helpers
  165. Holds view helpers that should be named like weblogs_helper.rb. These are generated
  166. for you automatically when using script/generate for controllers. Helpers can be used to
  167. wrap functionality for your views into methods.
  168. config
  169. Configuration files for the Rails environment, the routing map, the database, and other dependencies.
  170. db
  171. Contains the database schema in schema.rb. db/migrate contains all
  172. the sequence of Migrations for your schema.
  173. doc
  174. This directory is where your application documentation will be stored when generated
  175. using <tt>rake doc:app</tt>
  176. lib
  177. Application specific libraries. Basically, any kind of custom code that doesn't
  178. belong under controllers, models, or helpers. This directory is in the load path.
  179. public
  180. The directory available for the web server. Contains subdirectories for images, stylesheets,
  181. and javascripts. Also contains the dispatchers and the default HTML files. This should be
  182. set as the DOCUMENT_ROOT of your web server.
  183. script
  184. Helper scripts for automation and generation.
  185. test
  186. Unit and functional tests along with fixtures. When using the script/generate scripts, template
  187. test files will be generated for you and placed in this directory.
  188. vendor
  189. External libraries that the application depends on. Also includes the plugins subdirectory.
  190. If the app has frozen rails, those gems also go here, under vendor/rails/.
  191. This directory is in the load path.