Ruby on Rails Creating a Rails Application - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Ruby on Rails Creating a Rails Application

Description:

rake db:migrate. The rake command executes all the ruby files in ... Rake adds a ruby file to the db folder called schema. Lines beginning with # are comments. ... – PowerPoint PPT presentation

Number of Views:371
Avg rating:3.0/5.0
Slides: 14
Provided by: carol74
Category:

less

Transcript and Presenter's Notes

Title: Ruby on Rails Creating a Rails Application


1
Ruby on RailsCreating a Rails Application
  • Carol E Wolf
  • CS396X

2
Ruby Console Window
  • First choose Rails Applications
  • Next select Open Ruby Console Window
  • At the prompt, type
  • rails apps_name
  • For example rails library
  • This creates a full directory system in the
    folder rails_apps.
  • Console Window

3
Rails directory system
4
Important folders
  • app
  • controllers sits between client and database
  • helpers module with common ruby code
  • models code to interface with the database
  • views web pages containing html and erb
  • db contains database
  • migration methods to change the database
  • script
  • console interactive console window for ruby
    code
  • dbconsole interactive console window for the
    database
  • generate methods to generate scaffolding and
    migrations
  • server activates server to be used with
    localhost

5
Adding a table to the database
  • You can use the scaffold command to set up your
    database.
  • The database is created when you create the
    application.
  • Type the following to create a table called
    books.
  • ruby script/generate scaffold book isbnstring
    authorstring titlestring
  • Note that if you call it book, rails
    automatically makes it books.
  • This creates the file 20080805165427_create_books.
    rb in the db/migrate folder, where the numbers
    are a time stamp.
  • Then type
  • rake dbmigrate
  • The rake command executes all the ruby files in
    the migrate folder.
  • Rails includes a primary id field that is
    automatically incremented when you add rows to
    the database.

6
The file, schema.rb
  • Rake adds a ruby file to the db folder called
    schema. Lines beginning with are comments.
  • This file is auto-generated from the current
    state of the database.
  • (Additional comments omitted.)
  •  
  • ActiveRecordSchema.define(version gt
    20080805165427) do
  •  
  • create_table "books", force gt true do t
  • t.string "isbn"
  • t.string "author"
  • t.string "title"
  • t.datetime "created_at"
  • t.datetime "updated_at"
  • end
  •  end

7
View the contents of the database
  • To begin with, the books table is empty. Bring
    up the server to access it.
  • ruby script/server
  • The server that comes with InstantRails is called
    mongrel. It is a version of the open source
    apache server.
  • Point your browser to
  • http//localhost3000/books where localhost is
    the name for the IP address 127.0.0.1. (Use that
    if your computer does not recognize localhost.)
  • localhost is often called the local loop.

8
The first web page
  • The first web page is just the title of the
    table.
  • It includes a heading and a link to a New book
    page.
  • Click it to add data to the table.

9
The New book page
  • The new book page provides text fields with
    labels.
  • You can use these to add data.
  • Just fill in the fields and click Create.
  • When done, it will take you to a third page that
    shows what you added.

10
The show page and the index after adding a book
to the table.
11
The index.html.erb page uses embedded ruby code.
  • lth1gtListing bookslt/h1gt
  • lttablegt
  • lttrgt
  • ltthgtIsbnlt/thgt
  • ltthgtAuthorlt/thgt
  • ltthgtTitlelt/thgt
  • lt/trgt
  • lt for book in _at_books gt
  • lttrgt
  • lttdgtlth book.isbn gtlt/tdgt
  • lttdgtlth book.author gtlt/tdgt
  • lttdgtlth book.title gtlt/tdgt
  • lttdgtlt link_to 'Show', book gtlt/tdgt
  • lttdgtlt link_to 'Edit', edit_book_path(book)
    gtlt/tdgt
  • lttdgtlt link_to 'Destroy', book, confirm gt
    'Are you sure?', method gt delete gtlt/tdgt
  • lt/trgt
  • lt end gt
  • lt/tablegt

12
Explanation of some of the index page code
  • The lttablegt tag creates a table with headers
    enclosed by ltthgtlt/thgt , rows by lttrgtlt/trgt, and
    columns by lttdgtlt/tdgt
  • The embedded ruby code is enclosed by lthgt
  • lth book.isbn gt
  • This line displays the data in the book column.
  • The controller retrieves the data in a result
    set, a two dimensional array. It is displayed on
    the page using a for loop.
  • lt for book in _at_books gt
  • The _at_ sign is used to indicate a variable.
  • The lt link_to 'New book', new_book_path gt line
    generates the html code lta href"/books/new"gtNew
    booklt/agt .

13
Header code added by rails to the beginning of
the web pages. It also adds a foot to the end.
  • lt!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
    Transitional//EN" "http//www.w3.org/TR/xhtml1/DTD
    /xhtml1-transitional.dtd"gt
  • lthtml xmlns"http//www.w3.org/1999/xhtml"
    xmllang"en" lang"en"gt
  • ltheadgt
  • ltmeta http-equiv"content-type"
    content"text/htmlcharsetUTF-8" /gt
  • lttitlegtBooks indexlt/titlegt
  • ltlink href"/stylesheets/scaffold.css?121794806
    8" media"screen" rel"stylesheet"
    type"text/css" /gt
  • lt/headgt
  • ltbodygt
  • ltp style"color green"gtlt flashnotice gtlt/pgt
Write a Comment
User Comments (0)
About PowerShow.com