Active Resource on OSX

Posted by Joel Jensen Tue, 19 Dec 2006 17:05:00 GMT

I am making a site which uses an object datastore cache and rails. The only feasable way of getting data to rails is via XML. The simplest way is to use ActiveResource , which is still way way beta.

Here’s how I did it.

from http://reprocessed.org/blog/archives/2006/08/01/active_resource_on_edge.html

You may be tempted to use edge rails

rake rails:freeze:edge

This won’t work, ActiveResource is not included in Edge, only Trunk. Here is how to set up a rails instance that uses ActiveResource.

mkdir -p some_name/vendor; cd some_name
svn co http://dev.rubyonrails.org/svn/rails/trunk vendor/rails
rails .

Then, add this line to your config/environment.rb file, somewhere inside the Rails::Initializer.run do |config| block:

config.load_paths += %W( #{RAILS_ROOT}/vendor/rails/activeresource/lib )

You are running on trunk now, this is the latest version of rails, and may be flakey.

Here is how to use it

From http://weblog.techno-weenie.net/2006/12/13/taking-ares-out-for-a-test-drive

I created two rails sites on my local machine, one is the server. One is the client. ActiveResource needs to be on the client. On the server I made a RESTful site which hooked up to MySQL

Here is the Migration for the database.

class CreateParts < ActiveRecord::Migration
  def self.up
    create_table :parts do |t|
      t.column :date, :date
      t.column :customer, :string
      t.column :customer_part_number, :string
      t.column :manufacturer, :string
      t.column :factory_part_number, :string
      t.column :description, :string
      t.column :cost, :string
      t.column :selling_price, :string
      t.column :notes, :text
    end
  end

  def self.down
    drop_table :parts
  end
end

This command generates the restful scaffolding to view the data.

ruby script/generate scaffold_resource part date:date customer:string customer_part_number:string manufacturer:string factory_part_number:string description:string cost:string selling_price:string notes:text

Then on the client site I did the same. Except there is no database, just the previous classes.

In the Models section on the client, use this as your model for the part
class PartResource < ActiveResource::Base
  self.site = 'http://localhost:3000'
  # site.user = 'username'
  # site.password = 'secret_sauce'
end

class Part < PartResource
end

When I started each server up on a different port, the client can query the server and the server queries the database.

script/server lighttpd -p3000    # server
script/server lighttpd -p3001    # client

Pull up a page at http://localhost:3001/parts/1000 and check it out.

Enjoy

Comments

Leave a comment

Comments