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