Presentation is loading. Please wait.

Presentation is loading. Please wait.

Melbourne LUG Presentation Learning Rails and Ruby - making webapps easier.

Similar presentations


Presentation on theme: "Melbourne LUG Presentation Learning Rails and Ruby - making webapps easier."— Presentation transcript:

1 Melbourne LUG Presentation Learning Rails and Ruby - making webapps easier

2 Ruby...what is ruby? ● Its a: – dynamiclly-typed – garbage collecting – line-oriented – object-oriented – reflection-capable language ● Its derived from Perl and LISP ● Its ten years old

3 Do you do that in English? ● You write less code, a lot less code ● There isn't much boilerplate (e.g. ;) ● You can write large applications ● You can write natural DSLs ● Sadly though, its very, very slow

4 Now what about this Rails thing? ● Its a web framework ● Has a strong MVC focus ● Comes with AJAX, REST ad WS goodness ● Has a strong convention over configuration preference ● Used by the big boys: – BBC – LinkedIn – Twitter

5 I'm not convinced

6 What do you need? ● Any recent Linux, Mac, Solaris or even Windows machine – NB: Windows performance is awful ● A good text editor ● A web application idea :)

7 There are a couple of ways to get started... ● Instant rails – a package that gives you apache, rails, ruby and mysql – however windows only ● Distro packages – Ubuntu has ruby packages you can install, just add rails via gems – gems via apt locks you at old versions – see http://wiki.rubyonrails.org/rails/pages/RailsOnUbuntu

8 Compiling Ruby ● You'll need some tools to get started: – apt-get install ● build-essentials, zlib1gb, zlib1gb-dev – Get the latest ruby 1.8.6 from www.ruby-lang.org –./configure && make && sudo make install ● After that get rubygems from: – get rubygems (v1.0.1 or above) from rubyforge.org – sudo ruby setup.rb install

9 When do we get to make webapps?

10 Not yet, I just want to take a little detour ● gems – The ruby packaging system – Awesome versioning so you can peg to a version or mandate a minimum or maximum version – Basic dependancy checking – Use is simple via the gem command – Then simply use for your own nefarious purposes.

11 Detour continued... ● gems commands you need to know: – gem install - install a gem (may need sudo) – gem server – start documentation daemon – gem list --remote – find out what gems and versions are on the gem servers – gem query - search for a gem – gem update – update gems – gem uninstall - remove a gem

12 I'm turning to stone here!!!

13 OK OK here we go rails tickets – creates a rails project called tickets cd tickets && ruby script/server ● now you get a running app in a browser! ● erm...it doesn't do much though :( ● NB from here all paths are relative to tickets

14 Now let's hook to a database ● All databases live in config/database.yml ● there are three default databases, development, testing and production ● configuration is done via YAML ● edit the details to suit your database (postgresql) ● then run rake db:migratedb:migrate ● your database should now have a table called schema_info (this is important do not touch!)

15 Another detour...rake ● rake is the ruby equivalent of make ● it has dependancies and namespaces ● it has a cool DSL for tasks ● its all ruby so you can write ruby to extend it :) ● rake files are called Rakefile by default ● rake -T will tell you all the tasks a rakefile can execute ● We'll talk more about this later...

16 Now lets generate a data model ● Rails uses migrations to build its data model ● migrations are found in db/migrate ● migrations are numbered from 001 ● use the generation script: – ruby script/generate migration ● then edit the file... ● This gives you a great piecemeal development approach.

17 A migration file class CreateTicket < ActiveRecord::Migration def self.up create_table :tickets do |t| t.string :title t.text :description t.integer :severity end def self.down drop_table :tickets end

18 Now let's generate some more ● run the following: script/generate scaffold Ticket title:string description:text severity:integer ● now hit your web browser ● hey, I can make tickets!

19 Spot the changes in the database ● select * from schema_info; ● run rake db:migratedb:migrate ● whoa...new table called tickets ● select * from schema_info; ● see the number? ● but nothing uses it, yet

20 Create users ● OK now we'll do the same but for users: ● generate the migration and support files at once: script/generate scaffold User name:string active:boolean

21 Now we need to link them up ● We know there is a relationship between users and tickets. ● So lets implement it: ● Open your user model (app/models/user.rb) and inside the class definition put the following: has_many :tickets ● Then generate a migration script/generate migration link_to_users

22 Plugins ● Plugins are extensions to rails. ● This keeps rails small and light. ● Installation can be achieved by script/plugin install ● The software sits in the vendor/plugins directory. ● To install ActiveScaffold for AJAX scaffolds do: script/plugin install http://activescaffold.googlecode.com/svn/tags/active_scaffold

23 Now lets use ActiveScaffold ● Open app/views/layouts/users.html.erb ● Add the following inside the head tag: Then rip out the action code and add the following to app/controllers/users_controller.rb acive_scaffold :user

24 So simple even a politician could do it...

25 validation ● Rails comes with a set of validations pre- cooked. ● This makes for very expressive logic. For instance... ● Lets stop people making terse tickets ● Open app/models/ticket.rb and enter: validates_presence_of :description ● Now whenever you save a ticket Rails checks whether description is blank.

26 Test Driven Development/TDD ● TDD is the basic rails testing and its good. ● You write a test and then run: rake test ● Which runs all of your tests. ● We're only checking a model here so we run rake test:models ● next we open test/unit/ticket_test.rb

27 Testing a validation def test_blank_description test_ticket = Ticket.new test_ticket.title = "first test" test_ticket.save assert test_ticket.valid? end

28 What next? ● It fails! ● But that's OK... ● change the assert line to: assert_equal test_ticket.valid?, false ● Now the test passes and we know the validation works.

29 Some Of What We Haven't Talked About... ● Views ● Javascript, AJAX and bling ● Helpers ● BDD and rspec ● Mongrel ● HAML

30 Where to find out more? ● Check out: ● Books: – Ruby and Rails – David Black – The Pickaxe book ● Websites – www.rubyonrails.org www.rubyonrails.org – www.nubyonrails.com www.nubyonrails.com ● Melbourne Ruby Group (just google us).

31 Thanks to ● Flickr folk: – Libertinus – Annerna – Demona ● Development lords: – Matz & the Ruby maintainers – DHH & the Rails folk.


Download ppt "Melbourne LUG Presentation Learning Rails and Ruby - making webapps easier."

Similar presentations


Ads by Google