<?xml version="1.0" encoding="UTF-8"?>
<posts type="array">
  <post>
    <author-id type="integer">2</author-id>
    <body>I thought I'd continue the Facebooker theme, from my "previous post":http://gamutworks.net/posts/8, by extending it to include SMS. There is a lack of documentation on the Facebook developer wiki and Facebooker Rdocs, so here's a short, simple, high level how-to.

To keep things consistent, I'm going to be sticking with the same theme as the previous post. That is, reminding students about assignment deadlines.

h3. Assumptions

1) You've got a Facebook application up and running using Facebooker
2) Your app requires access to a Facebook user's profile
3) Your running OSX 10.5 or higher

h3. Lets go already

Open up the model that's handling your notifications and add the following:

&lt;pre lang="ruby"&gt;&lt;code&gt;
#app/models/assignment.rb

class Assignment &lt; ActiveRecord::Base
  belongs_to :course 

  named_scope :next, lambda { { :conditions =&gt; ['due_date &gt; ?', Date.today], :limit =&gt; 1 } }



  def self.send_sms
    fbook_sms_session = Facebooker::Mobile.new(Facebooker::Session.create)
    for user in User.all
      self.next.each do |a|
        fbook_sms_session.send(user, "#{a.title} is due in #{a.course.title} tomorrow.")
      end
    end 
  end
end
&lt;/code&gt;&lt;/pre&gt;

All this is doing, is creating a Facebooker mobile session, which is required when dealing with SMS, using the public class method, create, given to us by the Facebooker::Session class.

We then proceed to loop through all of our users, invoke our session, and call send, a public instance method of the Facebooker::Mobile class, which takes two params: 1) uids 2) a message.

At this point, you'll need to sign up for Facebook Mobile. This can be setup in your Facebook account settings.

Next, you need to ask your users to opt in to receive SMS from your application. To do this, add the following wherever you'd like in your views:

&lt;pre lang="ruby"&gt;&lt;code&gt;
&lt;%= fb_prompt_permission ('sms', "Click here if you'd like to receive SMS notifications from this application") %&gt;
&lt;/code&gt;&lt;/pre&gt;

Alright, now that we have everything in place we can test drive sending an SMS to our users. Load up script/console and fire the method:

&lt;pre lang="text"&gt;&lt;code&gt;
$ script/console
$ Assignment.send_sms
&lt;/code&gt;&lt;/pre&gt;

Voila!



</body>
    <category>Ruby on Rails</category>
    <created-at type="datetime">2010-02-02T05:26:56Z</created-at>
    <excerpt>Adding SMS features to your Facebook application with Rails.</excerpt>
    <id type="integer">10</id>
    <private type="boolean">false</private>
    <published-at type="datetime">2010-02-01T00:00:00Z</published-at>
    <tags>Facebooker, SMS, Ruby on Rails</tags>
    <title>Part 2: Adding SMS to your Facebooker Application</title>
    <updated-at type="datetime">2010-02-02T06:32:34Z</updated-at>
  </post>
  <post>
    <author-id type="integer">2</author-id>
    <body>For those of you who don't know, "Facebooker":http://facebooker.rubyforge.org/ is a Ruby wrapper over the Facebook REST API. Thanks to its contributors, building Facebook applications in Ruby has been made exceedingly simple.

The goal of this post is to send a notification to your Facebook application's users using Facebooker and running cron jobs. Let's get started.

h3. Assumptions

1) You've got a Facebook application up and running using Facebooker
2) Your app requires access to a Facebook user's profile
3) Your running OSX 10.5 or higher

If I've assumed too much, there is an exceptional Facebooker tutorial that walks you through how to install and initiate a Facebook application using Facebooker that can be found here: "http://apps.facebook.com/facebooker_tutorial/":http://apps.facebook.com/facebooker_tutorial/

h3. Setup

Install a gem called "whenever":http://github.com/javan/whenever:

&lt;pre lang="ruby"&gt;&lt;code&gt;
# config/environment.rb
config.gem 'javan-whenever', :lib =&gt; false, :source =&gt; 'http://gems.github.com'
&lt;/code&gt;&lt;/pre&gt;

&lt;pre lang="text"&gt;&lt;code&gt;
$ cd /my/rails/app
$ sudo rake gems:install
&lt;/code&gt;&lt;/pre&gt;

For most of us, writing cron jobs isn't terribly exciting nor is the syntax particularly friendly. The whenever gem allows you to write cron jobs using a clean Ruby syntax.

Next, we'll want to "wheneverize" our application. All this does is create an initial RAILS_ROOT/config/schedule.rb file for us to work from, complete with some great examples.

&lt;pre lang="text"&gt;&lt;code&gt;
$ cd /my/rails/app
$ wheneverize .
&lt;/code&gt;&lt;/pre&gt;

h3. Sending Facebook Notifications from Your Rails Application

There are two ways a Facebook application can send notifications: "user-to-user":http://wiki.developers.facebook.com/index.php/Notifications.send#User-to-user_Notifications and "application-to-user":http://wiki.developers.facebook.com/index.php/Notifications.send#Application-to-user_Notifications. For our purposes, we'll be working with application-to-user notifications. As the Facebook developer wiki notes, these are sent on the application's behalf and do not require an active session.

Okay, moving on. Next, we'll need to create a class method in the model you'd like to send notifications from. 

Facebooker provides us with a method called send_notification that we'll use to send our notifications. It takes two parameters: user_ids (an array) and some fbml. I'll be using an Assignment model that reminds students of upcoming deadlines for a given course:

&lt;pre lang="ruby"&gt;&lt;code&gt;
#app/models/assignment.rb

class Assignment &lt; ActiveRecord::Base
  belongs_to :course  

  def self.send_assignment_deadline
    Facebooker::Session.create.send_notification([User.all.map(&amp;:facebook_id).split(',')],"Holy Smokes, something is due today!")
  end
end
&lt;/code&gt;&lt;/pre&gt;

It's more than likely that you'll want your message to be dynamic, so here's how to do that:

&lt;pre lang="ruby"&gt;&lt;code&gt;
#app/models/assignment.rb
  def self.send_assignment_deadline
    self.each do |a|
      Facebooker::Session.create.send_notification([User.all.map(&amp;:facebook_id).split(',')], "#{a.title} is due in #{a.course.title} on &lt;b&gt;#{a.due_date.to_date}&lt;/b&gt;.")
    end
  end
 &lt;/code&gt;&lt;/pre&gt; 

Alright, lets load script/console and test this method.

&lt;pre lang="text"&gt;&lt;code&gt;
$ script/console
&gt;&gt; Assignment.send_assignment_deadline
=&gt; [#&lt;Assignment id: 38, title: "Et Aliquam", due_date: "2010-01-16 07:03:47", course_id: 4, created_at: "2010-01-17 18:31:36", updated_at: "2010-01-17 18:31:36"&gt;]
&lt;/code&gt;&lt;/pre&gt;

Awesome, moving on.

Next, we'll want to schedule some cron jobs to run using the RAILS_ROOT/config/schedule.rb file that wheneverize gave us.

At the top of your schedule.rb file, you have the option of setting the path to your Rails application. However, if you choose not to, the RAILS_ROOT will be used automatically.

Let's write a cron job using the Ruby syntax provided by the whenever gem and I'll explain it in a moment:

&lt;pre lang="ruby"&gt;&lt;code&gt;
#config/schedule.rb
set :environment, RAILS_ENV 

every :monday, :at =&gt; "12:00 am" do
  runner "Assignment.send_assignment_deadline"
end
&lt;/code&gt;&lt;/pre&gt;

The every block is used to indicate how often you'd like your job to run. This can be set in seconds, minutes, hours, days, weeks, whatever you'd like. For example if I wanted the same job to run daily, I could define an every block that looks like this:

&lt;pre lang="ruby"&gt;&lt;code&gt;
#config/schedule.rb
set :environment, RAILS_ENV 

every 1.day, :at =&gt; '12:00 am' do
  runner "Assignment.send_assignment_deadline"
end
&lt;/code&gt;&lt;/pre&gt;

Inside the block you're able to define multiple command, rake, and runner tasks appropriately. 

After your happy with your awesome job, execute the following command to update your local crontab file:

&lt;pre lang="text"&gt;&lt;code&gt;
$ whenever --update-crontab rails_app_name
&lt;/code&gt;&lt;/pre&gt;

You can check its contents by running:

&lt;pre lang="text"&gt;&lt;code&gt;
$ crontab -l
&lt;/code&gt;&lt;/pre&gt;

Note: If your trying to run this in production, you'll want to update your crontab accordingly. To do this:

&lt;pre lang="text"&gt;&lt;code&gt;
$ whenever --update-crontab rails_app_name --set environment=production
&lt;/code&gt;&lt;/pre&gt;

To edit the file, or create one if you don't already have one, run:

&lt;pre lang="text"&gt;&lt;code&gt;
$ crontab -e
&lt;/code&gt;&lt;/pre&gt;

And that's it. 

Your job(s) will run according to the specifications you've set out and be pushed to the users who have signed up for your Facebook application.

h3. Notes

There are limitations put in place by Facebook as to the number of notifications you can send a user in a given week. You can get these details for your Rails applications details by running the Facebooker public instance method:

&lt;pre lang="ruby"&gt;&lt;code&gt;
  get_allocation(integration_point)
  # Integration points include.. 
  # :notifications_per_day, :requests_per_day, :emails_per_day, :email_disable_message_location
&lt;/code&gt;&lt;/pre&gt;

Hopefully you've found this post useful in your endeavors to conquer the Ruby on Rails + Facebook world.

Credits:

Ryan Bates: "Cron in Ruby":http://railscasts.com/episodes/164-cron-in-ruby
"Whenever":http://github.com/javan/whenever</body>
    <category>Ruby on Rails</category>
    <created-at type="datetime">2010-01-13T03:15:01Z</created-at>
    <excerpt>Sending notifications from Facebooker using cron to your application's users.</excerpt>
    <id type="integer">8</id>
    <private type="boolean">false</private>
    <published-at type="datetime">2010-01-18T00:00:00Z</published-at>
    <tags>Ruby on Rails, Facebooker, Cron</tags>
    <title>Ruby on Rails Facebooker Notifications using Cron</title>
    <updated-at type="datetime">2010-01-27T02:02:01Z</updated-at>
  </post>
  <post>
    <author-id type="integer">2</author-id>
    <body>2010 is here and It's time for us to create and sustain a more active blog. We've garnered a respectable knowledge base since we opened up our Ruby on Rails web development shop in Edmonton in 2008, and it's time to start sharing what we know with the community. Starting today, January 4, 2010, we'll be blogging about the Ruby on Rails techniques we use, tips for designing simple user interfaces, how to write copy for the web, and an exciting project we're working on for the Edmonton web design and development community.

Check back on Monday mornings, twice a month, for updates. We look forward to you helping us make our new year blogging initiative a success by engaging in thoughtful, creative conversation with us.</body>
    <category></category>
    <created-at type="datetime">2010-01-03T23:02:53Z</created-at>
    <excerpt>2010 is here and It's time for us to create and sustain a more active blog. We've garnered a respectable knowledge base since we opened up our Ruby on Rails web development shop in Edmonton in 2008, and it's time to start sharing what we know with the community.</excerpt>
    <id type="integer">7</id>
    <private type="boolean">false</private>
    <published-at type="datetime">2010-01-04T00:00:00Z</published-at>
    <tags>ruby on rails, edmonton, web development, community</tags>
    <title>Happy New Year, Internet.</title>
    <updated-at type="datetime">2010-01-04T07:52:04Z</updated-at>
  </post>
  <post>
    <author-id type="integer">1</author-id>
    <body>&lt;iframe width="400" height="328" src="http://280slides.com/Viewer/?user=29083&amp;name=rails" style="border: 1px solid black; margin: 0; padding: 0;"&gt;&lt;/iframe&gt;</body>
    <category>Talk</category>
    <created-at type="datetime">2009-11-03T18:19:58Z</created-at>
    <excerpt>I've embedded the slide show portion of my presentation for Edmonton's ICE 2009. I'll be doing an intro to Ruby on Rails talk and demo this coming Wednesday (Nov 4th) from 10:15 to 11:30 am</excerpt>
    <id type="integer">6</id>
    <private type="boolean">false</private>
    <published-at type="datetime">2009-11-03T00:00:00Z</published-at>
    <tags>ICE 2009, Rails, Ruby on Rails</tags>
    <title>Presentation for ICE 2009</title>
    <updated-at type="datetime">2009-11-03T18:21:37Z</updated-at>
  </post>
  <post>
    <author-id type="integer">1</author-id>
    <body>I've been hooked on the Shoulda testing framework for Rails lately, as you may well know if you follow my "twitter":http://twitter.com/christiannaths feed.

I could go on and on about "Shoulda":http://www.thoughtbot.com/projects/shoulda/, but instead I'll let "Tammer Saleh":http://mtnwestrubyconf2008.confreaks.com/12saleh.html tell you about it himself. In short, it makes tests more readable.

No matter what framework you're writing your tests in, it's nice to know exactly what's going on, and exactly when it's happening. Whether you're writing development code or test code, it's imperative you know that what sort of effect the code you are writing is having on your application. That is the basic reasoning behind testing. As for how often you are told about these changes, well, normally you run &lt;em&gt;rake&lt;/em&gt; or &lt;em&gt;rake test&lt;/em&gt; from your command line, the entire test suite is run, and you get your results. Generally it takes a second or two and you can go back to coding.

Autotest changes your workflow by testing your &lt;em&gt;changes&lt;/em&gt; as they are saved. Again, you could be writing test code or development code; as the changes are saved, they are automatically tested. This can save you time and keystrokes. Just check in with your terminal window every time you save a line of code.

&lt;strong&gt;Wait, what?&lt;/strong&gt;

Yeah, that's a pile of window-switching going on. Let's get some help from Growl.

Autotest can work with the Growl Notifier to tell you when your tests are passing and failing. We can even sweeten the deal by utilizing the RedGreen gem. RedGreen colorizes your test output to help you more easily spot failing tests. So in this case, let's use it to colorize our Growl Notifications.

This setup really doesn't require Shoulda, so I'm not going to talk about the install and usage of Shoulda here, but I encourage you to &lt;a href="http://www.thoughtbot.com/projects/shoulda/"&gt;look into it further&lt;/a&gt; for own sanity.

first let's install autotest, it's a part of the ZenTest suite:

&lt;pre lang="text"&gt;&lt;code&gt;
$ sudo gem install ZenTest
&lt;/code&gt;&lt;/pre&gt;

you may notice if you try to run autotest, it's looking for the hoe gem, install it with:

&lt;pre lang="text"&gt;&lt;code&gt;
$ sudo gem install hoe
&lt;/code&gt;&lt;/pre&gt;

next, install redgreen:

&lt;pre lang="text"&gt;&lt;code&gt;
$ sudo gem install redgreen
&lt;/code&gt;&lt;/pre&gt;

now, just for kicks, path to your rails app and run autotest:

&lt;pre lang="text"&gt;&lt;code&gt;
$ autotest
&lt;/code&gt;&lt;/pre&gt;

at this point autotest should run your test suite and not return you to your prompt in terminal. Try editing some of your test code and save it. Now check back in with your command prompt; autotest should have run the associated tests. (colorized with redgreen)

So far so good, let's get Growl to help us out. In your home path (~/) let's edit (or create, if you have to) the .autotest file. I'm using Textmate so:

&lt;pre lang="text"&gt;&lt;code&gt;
$ mate ~/.autotest
&lt;/code&gt;&lt;/pre&gt;

In this file, let's add the following lines of code:

&lt;pre lang="ruby"&gt;&lt;code&gt;
  module Autotest::Growl
    def self.growl title, msg, img, pri=0, stick=""
      system "growlnotify -n autotest --image #{img} -p #{pri} -m '#{msg}' #{title} #{stick}"
    end
  
    Autotest.add_hook :red do |at|
      errors = at.files_to_test.map { |k, v| "#{k}:\n #{v.join("\n ")}"}.join("\n\n")
      #failed_tests = at.files_to_test.inject(0){ |s,a| k,v = a; s + v.size}
      growl "Tests failed:", "#{errors}", '~/library/autotest/rails_fail.png', 2
    end
  
    Autotest.add_hook :green do |at|
      res = at.results.scan(/Finished.*failures/m).to_s.gsub(/\e\[32m/,'')
      growl "Test Results", "#{res}", '~/library/autotest/rails_ok.png'
    end
  end
&lt;/code&gt;&lt;/pre&gt;

You may notice that we are relying on 3 things right now. The ability to use 'growlnotify' from the command line, and two image files: 'rails_fail.png' and 'rails_ok.png'. Let's get all 3 of these.

First, you can use growlnotify from the command line by adding the executable to your path. The executable is found in the &lt;a href="http://growl.info/"&gt;growl.dmg&lt;/a&gt; in the 'extras&gt;growlnotify' directory. Copy that file to your '/usr/local/bin' directory. Test it's been installed correctly by:

&lt;pre lang="text"&gt;&lt;code&gt;
$ growlnotify -m "testing growlnotify install" Growl Test
&lt;/code&gt;&lt;/pre&gt;

Now that we've got that working, download these two images:

&lt;img src="http://gamutworks.net/share/rails_ok.png" alt="rails_ok" /&gt;&lt;img src="http://gamutworks.net/share/rails_fail.png" alt="rails_fail" /&gt;

and save them to your '~/library/autotest' directory. Now run autotest and marvel at the fact that your just streamlined your testing workflow even further.

credits:

*&lt;a href="http://blog.codefront.net/2007/04/01/get-your-testing-results-via-growl-notifications/"&gt;blog.codefront.net&lt;/a&gt;
*&lt;a href="http://errtheblog.com/posts/13-colored-tests"&gt;errtheblog.com&lt;/a&gt;
*&lt;a href="http://blog.internautdesign.com/2006/11/12/autotest-growl-goodness"&gt;blog.internautdesign.com&lt;/a&gt;
*&lt;a href="http://maintainable.com/articles/dry_up_testing_with_autotest"&gt;maintainable.com&lt;/a&gt;</body>
    <category>Rails</category>
    <created-at type="datetime">2009-10-27T21:31:37Z</created-at>
    <excerpt>No matter what framework you're writing your tests in, it's nice to know exactly what's going on, and exactly when it's happening. Whether you're writing development code or test code, it's imperative you know that what sort of effect the code you are writing is having on your application. That is the basic reasoning behind testing.</excerpt>
    <id type="integer">4</id>
    <private type="boolean">false</private>
    <published-at type="datetime">2008-12-18T00:00:00Z</published-at>
    <tags>rails, testing, autotest, shoulda, RedGreen, growl</tags>
    <title>Rails Test-flow: Shoulda, Autotest, RedGreen, with Growl Notifications</title>
    <updated-at type="datetime">2009-11-01T06:57:02Z</updated-at>
  </post>
  <post>
    <author-id type="integer">1</author-id>
    <body>&lt;h3&gt;A Quick Look At Rails&lt;/h3&gt;

&lt;h3&gt;Part I - Overview&lt;/h3&gt;

&lt;h4&gt;What Rails is:&lt;/h4&gt;

&lt;acronym title="from rubyonrails.org"&gt;Rails is a full-stack framework for developing database-backed web applications according to the Model-View-Control (MVC) pattern. Ruby on Rails was created by David Heinemeier Hansson, a partner at 37signals, then extended and improved by a core team of committers and hundreds of open-source contributors.&lt;/acronym&gt; With Rails, all of the low level programming has been abstracted away, allowing you to be free to work on the higher level aspects of your web application

&lt;h4&gt;What Rails is not:&lt;/h4&gt;

Rails is not a substitute for solid programming knowledge and experience. It is not a magic 1 button tool to instantly create the worlds most perfect web application. Rails is not a substitute for planning and communication between developers.

&lt;h4&gt;What is Great About Rails:&lt;/h4&gt;

&lt;ul&gt;
	&lt;li&gt;Low level programming has been taken care of. For example, database connection, creation, and maintenance has become dead simple with Rake tasks.&lt;/li&gt;
	&lt;li&gt;It is built on Ruby, a very expressive and easy to use programming language. Ruby prides itself on readability; code that is easy understand is code that is easy to maintain.&lt;/li&gt;
	&lt;li&gt;The Rails community is phenomenal. There are many sites and blogs dedicated to Rails with a plethora of information answering virtually every Rails question you'll have.&lt;/li&gt;
	&lt;li&gt;Rails is plugin based. Re-use the code you've written by extracting it as a plugin, or better yet, use plugins that other people have written and save countless hours of coding. Most of Rails' really powerful features are found in various plugins.&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://api.rubyonrails.com/"&gt;Rails has a great API.&lt;/a&gt; For those really technical issues, the rails API is the place to find your answers.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Part II - Installation&lt;/h3&gt;

This is a quick overview of the process for Windows machines. Mac 0SX ships with Rails installed, so you can skip the rest of these steps; all you need to do is open Terminal and run:

&lt;pre lang="text"&gt;&lt;code&gt;
sudo gem update rails
&lt;/code&gt;&lt;/pre&gt;

But for us poor saps on Windows, we'll have do do the following:

&lt;ol&gt;
  &lt;li&gt;Download and Install &lt;a href="http://rubyforge.org/frs/?group_id=167"&gt;Ruby.&lt;/a&gt; At the time of this writing, we're looking for &lt;strong&gt;1.8.6-26 Final Release&lt;/strong&gt;. Download the .exe and run it from your desktop. Just follow the installer through using the default settings.&lt;/li&gt;
  &lt;li&gt;Download &lt;a href="http://rubyforge.org/frs/?group_id=126"&gt;rubygems.&lt;/a&gt; As of now, we'll grab &lt;strong&gt;rubygems-1.3.0.zip&lt;/strong&gt;. Extract this folder to the &lt;em&gt;C:ruby\bin directory&lt;/em&gt;. Then open the command line, navigate to &lt;em&gt;c:ruby\bin\rubygems-1.3.0&lt;/em&gt; and run&lt;/li&gt;
  &lt;li&gt;&lt;pre lang="text"&gt;&lt;code&gt;
c:&gt; cd ruby\bin\rubygems-1.3.0
c:ruby\bin\rubygems-1.3.0&gt; setup.rb
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
  &lt;li&gt;Once the incredibly verbose output is all finished, make sure rubygems is installed by running &lt;em&gt;gem -v&lt;/em&gt;. You should get the following output&lt;/li&gt;
  &lt;li&gt;&lt;pre lang="text"&gt;&lt;code&gt;
c:ruby\bin\rubygems-1.3.0&gt; gem -v
1.3.0
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
  &lt;li&gt;If however, you get an error message, it may be necessary to unistall Ruby (using the control panel), delete the c:ruby directory, and start at step 1 again. This should fix the issue.&lt;/li&gt;
  &lt;li&gt;Download &lt;a href="http://www.sqlite.org/download.html"&gt;sqlite3.&lt;/a&gt; On this page, we'll need to grab (found under 'Precompiled Binaries for Windows'), two files: &lt;strong&gt;sqlite-3_6_3.zip&lt;/strong&gt; and &lt;strong&gt;sqlitedll-3_6_3.zip&lt;/strong&gt;. Once you have these, extract both of the zip files into the &lt;em&gt;c:rubybin directory&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;Now, from the command line, run:&lt;/li&gt;
  &lt;li&gt;&lt;pre lang="text"&gt;&lt;code&gt;
c:ruby\bin\rubygems-1.3.0&gt; gem install --version1.2.3 sqlite3-ruby
&lt;/code&gt;&lt;/pre&gt;(the latest version of the sqlite3 gem hasn't been ported over to Windows yet, but this will work for us just fine)&lt;/li&gt;
  &lt;li&gt;Now, let's install Rails:&lt;/li&gt;
  &lt;li&gt;&lt;pre lang="text"&gt;&lt;code&gt;
c:ruby\bin\rubygems-1.3.0&gt; gem install rails
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
  &lt;li&gt;Lastly, we need to install a web server. Thankfully, it's not hard if we utilize rubygems:&lt;/li&gt;
  &lt;li&gt;&lt;pre lang="text"&gt;&lt;code&gt;
c:ruby\bin\rubygems-1.3.0&gt; gem install mongrel
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;Part III - A Test Application Walkthrough&lt;/h3&gt;

&lt;h4&gt;Lets create a new application&lt;/h4&gt;

First we need somewhere to put it:

&lt;pre lang="text"&gt;&lt;code&gt;
c:&gt; mkdir rails
c:&gt; cd rails
&lt;/code&gt;&lt;/pre&gt;

And create a new application with

&lt;pre lang="text"&gt;&lt;code&gt;
c:rails&gt; rails testapp
&lt;/code&gt;&lt;/pre&gt;

Lots of files and folders got created by using that command, we'll get to that. 

For now, lets scaffold out some sort of application, don't worry, we'll get into what scaffold is, but for now, just follow along. Lets say you want a way to keep track of your thousands of pairs of shoes. Let's make a shoe database

&lt;pre lang="text" style="overflow:auto;"&gt;&lt;code&gt;
c:rails&gt; cd testapp
c:rails\testapp&gt; ruby script/generate scaffold Shoe brand:string size:string
&lt;/code&gt;&lt;/pre&gt;

As you can see from the output, rails has created a whole bunch of files and folders. To take a better look, lets open up our text editor and have a peek. &lt;em&gt;By the way, for working with Rails on a Windows machine, I highly recommend &lt;a href="http://www.e-texteditor.com/"&gt;E Text Editor.&lt;/a&gt; It is a Textmate clone for Windows, and has some very powerful features, but also stays out of the way when you need it to.&lt;/em&gt;

&lt;h4&gt;MVC Architecture&lt;/h4&gt;

The files are arranged in folders using the MVC architecture - that is Model, View, Controller. MVC is an easy way to break up the code you are working on into logical sections. This makes it easier to find what you need (once you become familiar with MVC)

&lt;h5&gt;Models&lt;/h5&gt;

&lt;acronym title="from: http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/"&gt;Models are Ruby classes. They talk to the database, store and validate data, perform the business logic and otherwise do the heavy lifting. They?re the chubby guy in the back room crunching the numbers.&lt;/acronym&gt; In our case, we'll have a model for Shoe, and this model will retrieve our shoe data. (found in "models\shoe.rb")

&lt;h5&gt;Views&lt;/h5&gt;

The Views are what the user will see and interact with. They consist of embedded ruby (erb), where Ruby code is &lt;% mixed_in %&gt; with standard HTML. Lets take a look at some erb, open up layoutsshoes.html.erb

Code enclosed with these tags &lt;strong&gt;&lt;% %&gt;&lt;/strong&gt; is embedded Ruby. There are a few ways to embed Ruby into HTML; but there are two that are the most common. The first way is to guarantee some sort of output, for example when you want to put the value of a variable into the page:

&lt;pre lang="ruby"&gt;&lt;code&gt;
&lt;%= @title %&gt;
&lt;/code&gt;&lt;/pre&gt;

The second is when you do not want to have anything returned to the page. For example, when creating conditional statements or loop statements, where the output is contained in the block &lt;em&gt;between&lt;/em&gt; the erb.

&lt;pre lang="ruby"&gt;&lt;code&gt;
&lt;%- if user_logged_in? %&gt;
  &lt;div id="user-greeting"&gt;
    &lt;h2&gt;Hello &lt;%= @user.name %&gt;&lt;/h2&gt;
  &lt;/div&gt;
&lt;%- end %&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;h5&gt;Controllers&lt;/h5&gt;

Controllers are the brains of the operation. It consists of the ShoeController class and it's methods (or actions). You can write new actions here that will add functionality to your application, but scaffold has added a few for us: index, show, new, edit, create, update, and destroy. These 7 actions make up what is called a &lt;acronym title="Representational State Transfer"&gt;REST&lt;/acronym&gt;ful controller. &lt;a href="http://www.guardian.co.uk/technology/2007/dec/13/opensource.software"&gt;Learn more about REST in Rails and why it's important.&lt;/a&gt;

&lt;h5&gt;Helpers&lt;/h5&gt;

Wait, I thought it was MVC, not MVCH...? Well, helpers are great places to abstract code to. Code that is going to be used many times, and perhaps may be slightly verbose, is a good candidate for re-factoring to a helper. It's not a part of the MVC structure, but it comes in incredibly handy.

Let's take a quick break from the Rails framework and delve into Ruby...

&lt;h3&gt;A Quick Look at Ruby&lt;/h3&gt;

&lt;h4&gt;Part I - Try Ruby&lt;/h4&gt;

On of the best ways to learn Ruby is to get your hands dirty. And one of the best ways of doing that is found here: &lt;a href="http://tryruby.hobix.com/"&gt;http://tryruby.hobix.com/&lt;/a&gt;

&lt;h5&gt;Here we've learned:&lt;/h5&gt;

&lt;ul&gt;
	&lt;li&gt;Numbers and strings are Ruby's math and text objects.&lt;/li&gt;
	&lt;li&gt;Methods. You've used English-language methods like reverse and symbolic methods like * (the multiplication method.) Methods are action!&lt;/li&gt;
	&lt;li&gt;Errors. If you try to reverse a number or do anything fishy, Ruby will skip the prompt and tell you so.&lt;/li&gt;
	&lt;li&gt;Arrays are lists for storing things in order.&lt;/li&gt;
	&lt;li&gt;Variables save a thing and give it a name. You used the equals sign to do this.
  Like: ticket = [14, 37, 18].&lt;/li&gt;
	&lt;li&gt;Exclamations. Methods may have exclamations (and also question marks) in their name. No big deal. Try: poem.include? "my hand"&lt;/li&gt;
	&lt;li&gt;Square brackets. Target and find things. Search and replace.&lt;/li&gt;
	&lt;li&gt;Chaining methods lets you get a lot more done. Break up a poem, reverse it, reassemble it: poem.to_a.reverse.join&lt;/li&gt;
	&lt;li&gt;Hashes. The little dictionary with the curly pages: {}.&lt;/li&gt;
	&lt;li&gt;Symbols. Tiny, efficient code words with a colon: :splendid.&lt;/li&gt;
	&lt;li&gt;Blocks. Chunks of code which can be tacked on to many of Ruby's methods. Here's the code you used to build a scorecard:
  books.values.each { |rate| ratings[rate] += 1 }.&lt;/li&gt;
&lt;/ul&gt;




&lt;h4&gt;Part II - The Console&lt;/h4&gt;

Ok, now that we have that under out belt, let's break away and do some on our own. First open your terminal (command prompt in Windows) and type

&lt;pre lang="text"&gt;&lt;code&gt;
irb
&lt;/code&gt;&lt;/pre&gt;

This brings up our very own Ruby console. Lets try out some methods. Methods are like functions, we can store bits of Ruby code and re-use them over and over again.

Something simple

&lt;pre lang="text"&gt;&lt;code&gt;
&gt;&gt; def mymethod
&gt;&gt; puts "This is my very first method"
&gt;&gt; end
=&gt; nil
&lt;/code&gt;&lt;/pre&gt;

Now, call that method and see the output

&lt;pre lang="text"&gt;&lt;code&gt;
&gt;&gt; mymethod
=&gt; "This is my very first method"
&lt;/code&gt;&lt;/pre&gt;

A little more interactive:

&lt;pre lang="text"&gt;&lt;code&gt;
&gt;&gt; def mymethod(order)
&gt;&gt; puts "This is my #{order} method"
&gt;&gt; end
=&gt; nil
&lt;/code&gt;&lt;/pre&gt;

And the result

&lt;pre lang="text"&gt;&lt;code&gt;
&gt;&gt; mymethod("second")
=&gt; "This is my second method"
&lt;/code&gt;&lt;/pre&gt;
  
We'll play with these more later on in Rails.

&lt;h3&gt;More About the Ruby on Rails Framework&lt;/h3&gt;

&lt;h4&gt;Part I - Database and Rake Tasks&lt;/h4&gt;

Web applications need a database, Rails makes it easy with Rake tasks. Let's first take a look at a special file called 'database.yml' found in the config directory. This file contains our database credentials; it is used not only to establish the database connection every time Rails needs it, but also to create that connection initially.

Open the console and run

&lt;pre lang="text"&gt;&lt;code&gt;
testapp&gt; rake db:create:all
&lt;/code&gt;&lt;/pre&gt;

You've just created 3 new sqlite3 databases. One for development, one for testing, and one for production. The only one we are going to need right now is the one for development, but they are all ready to go for when you need them.

Databases need tables, and those tables need to have a specific schema, or structure. For example, our shoes need a table (called Shoes) with columns for ID, Brand and Size. We could create these manually, using an Sqlite3 gui, but rake tasks are so much more fun:

&lt;pre lang="text"&gt;&lt;code&gt;
testapp&gt; rake db:migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;h4&gt;Part II - Migrations&lt;/h4&gt;

Ok, but how did Rails know what tables and columns I wanted to create? Remember our scaffold command? Scaffold creates a file called a migration. (found in "dbmigrate") Let's take a peek at our _create_shoes.rb migration.

Now, if you decide later on that you don't need the column for size, but you would rather have columns for euro_size and us_size, you can edit this migration (or better yet, add a new one) to &lt;span style="text-decoration:line-through;"&gt;make the database your bitch&lt;/span&gt; bend the database to your will. Hint: check out the command 'ruby script/generate migration'

&lt;h3&gt;Part III - Routes&lt;/h3&gt;

Rails tells the web server (in our case we're using Mongrel) which pages to display and actions to use with a file called 'routes.rb' (found in the config directory). Open it up and have a peek. You'll notice at the top a line of code:

&lt;pre lang="ruby"&gt;&lt;code&gt;
  map.resources :shoes
&lt;/code&gt;&lt;/pre&gt;

This is part of the RESTfulness of scaffolding. The scaffold generator added this code to help the server serve the correct pages. For instance, because of this bit of Rails magic, the server knows that the URL /shoes is supposed to be directed to the /shoes/index page and fires the index action in the ShoesController. Likewise, the URL shoes/1 finds the shoes/show page and fires the show action in the ShoesController, while passing it the parameter '1'; this will find the shoe with the ID of 1 (if it exists).

&lt;h4&gt;Part IV - Your Server&lt;/h4&gt;

You may have noticed that all of this is relying on the webserver. Let's start our server and test out our new app.

&lt;pre lang="text"&gt;&lt;code&gt;
testapp&gt;ruby script/server
&lt;/code&gt;&lt;/pre&gt;

Your command prompt has now been transformed into the output for the web server. Now you can see exactly what's going on behind the scenes and you click through pages and submit forms. By default, Rails will run on port 3000 of your localhost. Open up a web browser and type in the URL @http://localhost:3000/shoes@</body>
    <category>Rails</category>
    <created-at type="datetime">2009-10-27T20:53:31Z</created-at>
    <excerpt>Rails is a full-stack framework for developing database-backed web applications according to the Model-View-Control (MVC) pattern. Ruby on Rails was created by David Heinemeier Hansson, a partner at 37signals, then extended and improved by a core team of committers and hundreds of open-source contributors.

With Rails, all of the low level programming has been abstracted away, allowing you to be free to work on the higher level aspects of your web application.</excerpt>
    <id type="integer">2</id>
    <private type="boolean">false</private>
    <published-at type="datetime">2008-09-30T00:00:00Z</published-at>
    <tags>rails, windows, install</tags>
    <title>DMD on Rails - Session 1 (Ruby/Rails/MVC Architecture)</title>
    <updated-at type="datetime">2009-11-01T07:18:56Z</updated-at>
  </post>
  <post>
    <author-id type="integer">1</author-id>
    <body>bq. _UPDATE:_ Lovd By Less is now using Sphinx instead of ferret, so we can no longer provide help on the instructions found here. We'll leave this post up for use as a guide, but make sure you ingore the part on installing Ferret in this post and refer to the "Lovd readme":http://github.com/stevenbristol/lovd-by-less/ for the proper instructions on installing Sphinx. 



On doing a recent test-run of the wonderful "Lovd By Less":http://lovdbyless.com/ I ran into a few system dependent problems. Yes, I'm running this on Windows. (Boo, hiss). I know, I know, let's move on.

I &lt;a href="http://lovdbyless.com/download.html"&gt;downloaded&lt;/a&gt; a fresh batch of Lovd, and slapped it into a brand-spanking new rails app. The most important thing here is to follow the readme provided by Lovd... which works great if you're on a Mac. I found they leave a few things out for us poor saps on Windows.

&lt;h4&gt;First. Update Ruby Gems.&lt;/h4&gt;

Surprisingly harder than it sounds. You need to be running RubyGems1.2.0. To upgrade from RubyGems1.1.1 open a command prompt to your root directory and type the following:

&lt;pre lang="text"&gt;&lt;code&gt;&gt; gem install rubygems-update -v 1.1.1
&gt; gem update --system&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;Second. Install ImageMagick &amp;amp; RMagick.&lt;/h4&gt;

Go &lt;a href="http://rubyforge.org/frs/?group_id=12&amp;amp;release_id=22755"&gt;here&lt;/a&gt; and get the rmagick-win32 bundle (near the bottom). This .zip file contains the proper version of ImageMagick; it has to be ImageMagick version 6.4.1, no other version will work.

Now extract the .zip and install ImageMagic from the pre-compiled install file (.exe). Next, you'll notice the .zip came packed with a file called 'rmagick-2.5.2.gem'. Put this file in c:\ruby\rubygems, and cd to that directory with your command prompt and type:

&lt;pre lang="text"&gt;&lt;code&gt;&gt; gem install RMagick --local&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;Third. Update a million gems.&lt;/h4&gt;

To be completely thorough, go through and do a 'gem install' for each of the following gems:

&lt;pre lang="text"&gt;&lt;code&gt;will_paginate
colored
youtube-g
uuidtools
acts_as_ferret
ferret
hpricot
mocha
redgreen
avatar
win32console
RedCloth
tzinfo
flickr
mime-types&lt;/code&gt;&lt;/pre&gt;

You might notice that 'gem install ferret' doesn't seem to work.

&lt;h4&gt;Fourth. Go Get Ferret. &lt;em&gt;this step has been deprecated with the latest release of Lovd By Less&lt;/em&gt;&lt;/h4&gt;

You're going to have to go to the &lt;a href="http://rubyforge.org/frs/?group_id=1028"&gt;source&lt;/a&gt; and get the latest .gem for ferret. Download it and put it in c:\ruby\rubygems, go to that directory in your command prompt and type:

&lt;pre lang="text"&gt;&lt;code&gt;&gt; gem install ferret-0.11.6-mswin32.gem&lt;/code&gt;&lt;/pre&gt;

Now you might be tempted to take your new social network out for a spin, a couple more steps before you do.

&lt;h4&gt;Fifth. Edit environment.rb&lt;/h4&gt;

Yeah, I'm not 100% why this is, but it appears there are some errors in this file. Open config/environment.rb in your favorite text editor. Near the bottom you will see many required gems, change them so they read:

&lt;pre lang="ruby"&gt;&lt;code&gt;config.gem 'will_paginate', :version =&gt; '~&gt; 2.2.2'
config.gem 'colored', :version=&gt; '1.1'
config.gem 'youtube-g', :version=&gt; '0.4.1', :lib=&gt;'youtube_g'
config.gem 'uuidtools', :version=&gt; '1.0.3'
config.gem 'acts_as_ferret', :version=&gt; '0.4.3'
config.gem 'ferret', :version=&gt; '0.11.4'
config.gem 'hpricot', :version=&gt; "0.6"
config.gem 'mocha', :version=&gt; "0.5.6"
config.gem 'redgreen', :version=&gt; "1.2.2"
config.gem 'avatar', :version=&gt; "0.0.5", :lib =&gt; 'avatar'&lt;/code&gt;&lt;/pre&gt;

&lt;h4&gt;Sixth. Windows Environment.&lt;/h4&gt;
This was the hardest part to figure out, but apparently in Windows, Ruby will try to run some of the gems before running RubyGems itself, resulting in an error. To help Windows out, open 'control panel' &gt; 'system' and click on 'advanced system settings'. Next click on the 'Evnvironment Variables' button and add a new User and System variable; Variable name: RUBYOPT, Variable value: -rubygems.

Now reboot your system.

&lt;h4&gt;Seventh. Rake and Run.&lt;/h4&gt;

Open your command prompt, rake your database and run your server, open your browser to localhost:3000 and you should have your own version of Lovd before your eyes. Now wasn't that easy? (gulp).

&lt;strong&gt;Disclaimer:&lt;/strong&gt;&lt;em&gt; some of you may have noticed that it seems some parts to this guide are left out; this is not a complete guide, but it should cover what's been left out of the Lovd By Less readme file. I'm not pointing fingers at the Lovd team, these are just a few problems that cropped up for me that I thought I'd share.&lt;/em&gt;</body>
    <category>Rails</category>
    <created-at type="datetime">2009-10-27T21:30:15Z</created-at>
    <excerpt>On doing a recent test-run of the wonderful Lovd By Less I ran into a few system dependent problems.

I downloaded a fresh batch of Lovd, and slapped it into a brand-spanking new rails app. The most important thing here is to follow the readme provided by Lovd... which works great if you're on a Mac. I found they leave a few things out for us poor saps on Windows.</excerpt>
    <id type="integer">3</id>
    <private type="boolean">false</private>
    <published-at type="datetime">2008-07-22T00:00:00Z</published-at>
    <tags>lovd by less, windows</tags>
    <title>Installing Lovd By Less on Windows (XP &amp; Vista)</title>
    <updated-at type="datetime">2009-11-01T06:52:01Z</updated-at>
  </post>
</posts>
