22
Ruby on Rails Quick Tip #1: Loading models in Rails 2.0
Here is a quick tip, that i have learned, this applied to the book Beginning Ruby on Rails chapter 7 (page 206), in the book we were asked to load models (please remember that the book was published when the rails being used was 1.2.X, around 2007.
# Ruby on Rails - Quick Tip - Loading models in Ruby 2.X.X
# Book: Beginning Ruby on Rails Chapter 7 (page 206)
# Marco's notes: 2009.03.23
# We were asked to load models in the application controller
class ApplicationController < ActionController::Base
model :cart
model :purchases
end
# Results in this:
# undefined method `model' for ApplicationController:Class
# This ofcourse won't work in Ruby 1.8.6 (universal-darwin9.0)
# and Rails version 2.3.2
# So we try this:
class ApplicationController < ActionController::Base
require_dependency 'cart' # this solves the issue load :model
require_dependency 'purchase' # this solves the issue load :model
end
By the way, for those who have this book, the official forum for it is: Beginning Ruby on Rails.
Learn more about require_dependency .
I hope it was useful.
22
Ruby on Rails: Scaffold before and after 2.0
Here we are, i have spent quite some time figuring this out, hopefully google is our friend and patient as well, this example comes from doing the excelent Beginning Ruby on Rails book from Wrox, chapter 6 page 167.
Please read the comments on the code:
# Ruby on Rails - Learnt by example, Scaffold issue old vs new # Book: Beginning Ruby on Rails Chapter 6 (page 167) # Marco's notes: 2009.03.23 # In step 2 - Create a model named Item and a controller named Manage ... # Original code, as run by the Ruby 1.8.4 and Rails 1.2.1 (circa 2006) ruby script/generate scaffold Item Manage # Results in this: wrong number of arguments (1 for 2) # Topic available here: # http://groups.google.com/group/bangalorerug/browse_thread/thread/c08b0dccd17f06d1 # This ofcourse won't work in Ruby 1.8.6 (universal-darwin9.0) # and Rails version 2.3.2 # So we try this: # ruby script/generate scaffold model field:fieldType ... ruby script/generate scaffold Item name:string description:text price:float # This solves the issue in page 167, but remember that now your controller will be # items, not manage, so in the example would be http://localhost:3000/items
See, there you go, remember that after doing this, in your chapter 6, you won’t access your scaffold result by going to http://localhost:3000/manage but http://localhost:3000/items.
Beginning Ruby on Rails – Chapter 6 Wrong number of arguments.
Also a nice tutorial, although a bit long is: Rails 2.0 step by step.
Hope this has helped.




