background preloader


Facebook Twitter

Rolling with Ruby on Rails Revisited, Part 2. By Bill Walton 01/05/2007 Editor's note: this is the second part of Rolling with Ruby on Rails Revisited, an update to Curt Hibbs's influential Rolling with Ruby on Rails and Rolling with Ruby on Rails Part 2.

Rolling with Ruby on Rails Revisited, Part 2

Also check out Bill Walton's monthly series, Cookin' With Ruby on Rails. NOTE TO READER: This tutorial is intended for use with Rails 1.2.6. It has not yet been updated to support Rails 2.x. Paul: Good pizza. Boss: Actually, Paul, I'm pretty interested in seeing that myself. CB: No problem. Figure 12. Boss: That's quite a bit of code, and I get most of it. CB: That's one of the differences between a real web-based application and site that's just a bunch of static pages with some scripting, Boss. Boss: OK. Paul: Sure, Boss. CB: OK, then. I tell Rails about the relationship between recipes and categories in their models. Has_many :recipes Figure 13. Belongs_to :category Figure 14.

Now Rails knows about the relationship between the two models. Figure 15. CB: Keen eye, Paul. RailsPlayTime. Ruby on Rails Documentation. Intermediate Rails: Understanding Models, Views and Controllers. I’m glad people liked the introduction to Rails; now you scallawags get to avoid my headaches with the model-view-controller (MVC) pattern.

Intermediate Rails: Understanding Models, Views and Controllers

This isn’t quite an intro to MVC, it’s a list of gotchas as you plod through MVC the first few times. Here’s the big picture as I understand it: The browser makes a request, such as web server (mongrel, WEBrick, etc.) receives the request. It uses routes to find out which controller to use: the default route pattern is “/controller/action/id” as defined in config/routes.rb. In our case, it’s the “video” controller, method “show”, id “15″. It’s more fun to imagine a story with “fat model, skinny controller” instead of a sterile “3-tiered architecture”. Many MVC discussions ignore the role of the web server.

SuperModels Models are fat in Railsville: they do the heavy lifting so the controller stays lean, mean, and ignorant of the details. Using ActiveRecord class User < ActiveRecord::Base end Ruby can also handle “undefined” methods with ease. Becomes. What is ruby? String. Str % arg → new_str click to toggle source Format—Uses str as a format specification, and returns the result of applying it to arg.


If the format specification contains more than one substitution, then arg must be an Array or Hash containing the values to be substituted. See Kernel::sprintf for details of the format string. static VALUE rb_str_format_m(VALUE str, VALUE arg) { volatile VALUE tmp = rb_check_array_type(arg); if (! NIL_P(tmp)) { return rb_str_format(RARRAY_LENINT(tmp), RARRAY_PTR(tmp), str); } return rb_str_format(1, &arg, str); } str * integer → new_str click to toggle source Copy—Returns a new String containing integer copies of the receiver.

"Ho! Str + other_str → new_str click to toggle source Concatenation—Returns a new String containing other_str concatenated to str. "Hello from " + self.to_s str << integer → str click to toggle source str << obj → str Append—Concatenates the given object to str. A = "hello "a << "world" a.concat(33) str[fixnum, fixnum] → new_str or nil produces: