A Django site.
February 16, 2009

Learning and Corporate Development - Entries
Learning and Corporate Development - Entries
Learning and Corporate Development - Home is about »
» My First Rails Applicaiton

This tutorial shows how to "scaffold" very basic forms in rails.

Create a folder that will hold your web site, I created a folder called "Sites" within my home directory.

Press apple key then space bar to start up spot light Start typing "Terminal", once it has been found you can click to run

spot light

Within Terminal Type

cd sites
rails inform
cd inform

Directory will be changed to 'sites'. A new rails application folder named 'inform' will be created, copying all required files into place. Directory will be changed to 'inform'.

The next step is to 'scaffold' 'notice': (when using 'scaffold' do not use plurals)

./script/generate scaffold notice title:string content:string userid:string

This will generate all the files and code required to view, edit and delete notices.

Now that all the files and code are in place the database needs to be created, by default ruby will use a flat db file for testing.

Within Terminal type:

rake db:create
rake db:migrate
script/server

This will create the database and the notices table. 'script/server' will start up the local web service.

Open a browser window and type:

http://localhost:3000/notices

This is the index page:

notices index

New:

notices index

Show:

notices index

Edit:

notices index

Index after Item added:

notices index

Press 'crtl & c' within terminal to stop web service.

» Cucumber Tutorial - Ruby on Rails

The tutorial will show you how to install what is required to run cucumber then work through a simple demonstration. The tutorial will create a test that checks a page exists and contains some text.

How to install: RSPEC

mkdir -p ~/Library/Application\ Support/TextMate/Bundles/ cd ~/Library/Application\ Support/TextMate/Bundles git clone git://github.com/bmabey/rspec-story-tmbundle.git RSpec\ Story.tmbundle

How to install: WEBRAT

mkdir -p ~/Library/Application\ Support/TextMate/Bundles/ cd ~/Library/Application\ Support/TextMate/Bundles git clone git://github.com/bmabey/webrat-tmbundle.git Webrat.tmbundle

How to install: Cucumber

mkdir -p ~/Library/Application\ Support/TextMate/Bundles/ cd ~/Library/Application\ Support/TextMate/Bundles git clone git://github.com/bmabey/cucumber-tmbundle.git Cucumber.tmbundle

Sudo gem install rspec-rails cucumber webrat

Create a new rails application

CD sites Rails Hey CD Hey Ruby Script/Generate cucumber mate .

Menu Bundles / Bundle Editor / Reload Bundles Open the Bundle Editor / Filter Button / Uncheck RSPEC

Create a new file

/features/Homepage.feature

Open the file, press ctrl Apple T - Type Feature Select Feature - cucumber

Also do this with Scenario - cucumber

Output to the file is:

Feature: title In order to value As a role I want feature

Scenario: title Given context When event Then outcome

Change this to:

Feature: view homepage In order to love the website As a viewer I want to see a beautiful page

Scenario: visit homepage Given Im on the "homepage" Then I should see "Beauty"

note-- we have used the words "I should see" as it is a method that exists in the webrat_steps.rb file. There are many commonly used webrat steps within this file. using these saves us a lot of time:

Then /^I should see "(.*)"$/ do |text| response.body.should =~ /#{text}/m end

As Beauty is in quotes it is passed as the parameter

Apple R will run it and show that the first step is pending

Run:

script/cucumber features

This will return:

You can use these snippets to implement pending steps which have no step definition:

Given /^Im on the "homepage"$/ do end

This tells us what to put in our step file:

So create our step definition:

/features/Step_definitions/site_steps.rb

Given /^Im on the "homepage"$/ do visit "/sites" end

Go back to the features file and apple R

Now the test fails as no page exists

Now that we have a test that fails we need to create the homepage file so that the test can pass

in terminal:

script/generate controller sites index

outputs:

mac1:hey Tyler6699$ script/generate controller sites index exists app/controllers/ exists app/helpers/ create app/views/sites exists test/functional/ create app/controllers/sites_controller.rb create test/functional/sites_controller_test.rb create app/helpers/sites_helper.rb create app/views/sites/index.html.erb mac1:hey Tyler6699$

Go to the feature file and press Apple R:

The page should now be found:

Edit file:

/app/views/sites/index.html.erb

add this line of code:

<p>Beauty</p>

Apple R from Feature File:

The test now passes as the word has been found.

Run this from terminal:

script/cucumber features

outputs:

j270-mac1:hey Tyler6699$ script/cucumber features Feature: view homepage # features/Homepage.feature In order to love the website As a viewer I want to see a beautiful page Scenario: visit homepage # features/Homepage.feature:6 Given Im on the "homepage" # features/step_definitions/site_steps.rb:1 Then I should see "Beauty" # features/step_definitions/webrat_steps.rb:89

1 scenario 2 steps passed

Read through the webrat rb file "webrat_steps.rb" for all of the pre defined methods such as:

When /^I press "(.*)"$/ do |button| click_button(button) end

When /^I follow "(.*)"$/ do |link| click_link(link) end

When /^I fill in "(.*)" with "(.*)"$/ do |field, value| fill_in(field, :with => value) end

This will allow you to build more complex tests.