Ruby on Rails Creating a Rails Application

Ruby on Rails Creating a Rails Application Carol E Wolf CS396X 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 Rails directory system Important folders app

db contains database 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 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 Adding a table to the database You can use the scaffold command to set up your database. Type the following to create a table called books.

ruby script/generate scaffold book isbn:string author:string title:string 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 The database is created when you create the application. rake db:migrate 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. 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.) ActiveRecord::Schema.define(:version => 20080805165427) do create_table "books", :force => true do |t| t.string "isbn" t.string "author" t.string "title" t.datetime "created_at" t.datetime "updated_at" end end

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://localhost:3000/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. 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. 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. The show page and the index after adding a book to the table. The index.html.erb page uses embedded ruby code.

Listing books

`

<% for book in @books %>

<% end %>

Isbn Author Title
<%=h book.isbn %> <%=h book.author %> <%=h book.title %> <%= link_to 'Show', book %> <%= link_to 'Edit', edit_book_path(book) %> <%= link_to 'Destroy', book, :confirm => 'Are you sure?', :method => :delete %>

<%= link_to 'New book', new_book_path %> Explanation of some of the index page code The

tag creates a table with headers enclosed by

, rows by

, and columns by

The embedded ruby code is enclosed by <%=h%>

The controller retrieves the data in a result set, a two dimensional array. It is displayed on the page using a for loop. <%=h book.isbn %> This line displays the data in the book column. <% for book in @books %> The @ sign is used to indicate a variable. The <%= link_to 'New book', new_book_path %> line generates the html code New book .

Header code added by rails to the beginning of the web pages. It also adds a foot to the end. Books: index

<%= flash[:notice] %>

Recently Viewed Presentations

  • Client&#x27;s Logo/Name

    Client's Logo/Name

    Lecture 11 MARK2039 Summer 2006 George Brown College Wednesday 9-12
  • ANALYSIS OF FIELD RETURN SET Applicable Model :

    ANALYSIS OF FIELD RETURN SET Applicable Model :

    Applicable for 245 Ltr & 285 Ltr model of KAS factory. Model start with VAL254,VAL294,KSL254,KSL294. ... Put pressure from cond outlet & check the removal near U trap location. The tube from where the pressure is coming out will be...
  • SWE 637: Graph Coverage

    SWE 637: Graph Coverage

    WAFs/XSS filters check patterns in request parameters via regular expression matching. No script, no event handler present. No escape involved. Some gargets even allow encoding payloads. ... SWE 637: Graph Coverage Last modified by: Microsoft Office User
  • Liar - English

    Liar - English

    Questions The poem is a complicated exploration of a cross dresser and a habitual liar. But it is also about how we judge people and whether we have the right to judge them.
  • Catford enterprise hub feasibility report

    Catford enterprise hub feasibility report

    Lewisham Town Hall Feasibility Final Report Draft July 2014 tom fleming / creative consultancy / Introduction tom fleming / creative consultancy / 1: Demand and Context for an Incubation and workspace in Catford Town Hall and Lewisham tom fleming /...
  • PISCES - CORA Home

    PISCES - CORA Home

    PISCES will provide an invaluable test-drive as to how other marine regions may be managed in the future, as emerging policies are put into practice. We will maintain close interactions with industry, government, agencies and related projects across the UK,...
  • Presentation Title Runs Here Title Runs Here

    Presentation Title Runs Here Title Runs Here

    promoting and safeguarding the health, welfare and safety of pupils. working in partnership with parents, support staff and other professionals. undertaking appropriate and agreed continuing professional development. participating in issues related to school planning, raising achievement and individual review
  • Disclosure in Cartel Cases in Canada

    Disclosure in Cartel Cases in Canada

    Canadian disclosure rules in criminal cases. Criminal procedures apply to cartel offences "All relevant information" must be disclosed (R v Stinchcombe - SCC)Both inculpatory and exculpatory evidence