Lecture 7 - Ruby on...

14
CS 390 Software Engineering Lecture 7 Ruby on Rails References: Getting Started With Rails , available at https://guides.rubyonrails.org/getting_started.html

Transcript of Lecture 7 - Ruby on...

Page 1: Lecture 7 - Ruby on Railsuenics.evansville.edu/~hwang/f18-courses/cs390/Lecture07-Ruby-on-Rails.pdf · Ruby on Rails Rails is a web application development framework written in the

CS 390 Software Engineering

Lecture 7 – Ruby on Rails

References: Getting Started With Rails, available at https://guides.rubyonrails.org/getting_started.html

Page 2: Lecture 7 - Ruby on Railsuenics.evansville.edu/~hwang/f18-courses/cs390/Lecture07-Ruby-on-Rails.pdf · Ruby on Rails Rails is a web application development framework written in the

Outline

Finish Ruby language basics

Ruby on Rails

CS 390 Software Engineering - Lecture 7Septebmer 7, 2018 2

Page 3: Lecture 7 - Ruby on Railsuenics.evansville.edu/~hwang/f18-courses/cs390/Lecture07-Ruby-on-Rails.pdf · Ruby on Rails Rails is a web application development framework written in the

Ruby scripts

Look at ri20min.rb. First line is

#!/usr/bin/env ruby

Comments start with # to the end of the line

Start of "main program" is if __FILE__ == $0 to the

reserved word end. __FILE__ is an environment

variable set to the name of this file. $0 is the first

command-line argument (i.e. argv[0]). If they are the

same, then this file is being run as a program. Otherwise

it is being used as a library and this code is ignored.

Run script using: ruby ri20min.rb

CS 390 Software Engineering - Lecture 7Septebmer 7, 2018 3

Page 4: Lecture 7 - Ruby on Railsuenics.evansville.edu/~hwang/f18-courses/cs390/Lecture07-Ruby-on-Rails.pdf · Ruby on Rails Rails is a web application development framework written in the

Ruby objects

A Ruby object has Smalltalk-like behavior and can be

asked if it responds to a particular method name.

if @names.respond_to?("each")

E.g., this can be used to distinguish between single

objects and collection objects.

Collections have many methods such as each and

join.

CS 390 Software Engineering - Lecture 7Septebmer 7, 2018 4

Page 5: Lecture 7 - Ruby on Railsuenics.evansville.edu/~hwang/f18-courses/cs390/Lecture07-Ruby-on-Rails.pdf · Ruby on Rails Rails is a web application development framework written in the

Arrays

Arrays can be defined explicitly giving a list in [ ].

mg.names = ["Albert", "Brenda", "Charles",

"Dave", "Engelbert"]

See course webpage for additional Ruby language

tutorials and documentation.

CS 390 Software Engineering - Lecture 7Septebmer 7, 2018 5

Page 6: Lecture 7 - Ruby on Railsuenics.evansville.edu/~hwang/f18-courses/cs390/Lecture07-Ruby-on-Rails.pdf · Ruby on Rails Rails is a web application development framework written in the

Ruby on Rails

Rails is a web application development framework

written in the Ruby programming language designed to

make programming web applications easier by making

assumptions about what every developer needs to get

started and generating the appropriate files.

It assumes applications will be written using a Model-

View-Controller pattern. Doing so generally leads to

increased productivity. Not doing so generally leads to

frustration.

CS 390 Software Engineering - Lecture 7Septebmer 7, 2018 6

Page 7: Lecture 7 - Ruby on Railsuenics.evansville.edu/~hwang/f18-courses/cs390/Lecture07-Ruby-on-Rails.pdf · Ruby on Rails Rails is a web application development framework written in the

Model-View-Controller pattern

Model – data structures that define state of application and algorithms to manipulate data.

View – presentation of model in some format.

Controller – receives user input and manipulates the model.

Septebmer 7, 2018 CS 390 Software Engineering - Lecture 7 7

From: https://en.wikipedia.org/wiki/Model-view-controller

Page 8: Lecture 7 - Ruby on Railsuenics.evansville.edu/~hwang/f18-courses/cs390/Lecture07-Ruby-on-Rails.pdf · Ruby on Rails Rails is a web application development framework written in the

Rails philosophy

Don't Repeat Yourself: DRY is a principle of software

development which states that "Every piece of

knowledge must have a single, unambiguous,

authoritative representation within a system." By not

writing the same information over and over again, code

is more maintainable, more extensible, and less buggy.

Convention Over Configuration: Rails has opinions

about the best way to do many things in a web

application, and defaults to this set of conventions, rather

than require that a programmer specify minutiae through

endless configuration files.

CS 390 Software Engineering - Lecture 7Septebmer 7, 2018 8

Page 9: Lecture 7 - Ruby on Railsuenics.evansville.edu/~hwang/f18-courses/cs390/Lecture07-Ruby-on-Rails.pdf · Ruby on Rails Rails is a web application development framework written in the

Rails application

A Rails application must have at least one controller and

one view.

▪ A controller receives specific requests for the application.

Routing decides which controller receives which requests. There

may be more than one route to each controller, and different

routes can be served by different actions. Each action's purpose

is to collect information to provide it to a view.

▪ A view displays this information in a human readable format. An

important distinction to make is that it is the controller, not the

view, where information is collected. The view should just display

that information. By default, view templates are written in a

language called eRuby (Embedded Ruby) which is processed by

the request cycle in Rails before being sent to the user.

CS 390 Software Engineering - Lecture 7Septebmer 7, 2018 9

Page 10: Lecture 7 - Ruby on Railsuenics.evansville.edu/~hwang/f18-courses/cs390/Lecture07-Ruby-on-Rails.pdf · Ruby on Rails Rails is a web application development framework written in the

Creating a new application

Rails comes with scripts called generators that are

designed to make development easier by creating

everything that's necessary to start working on a

particular task.

New application generator will provide the foundation of

a fresh Rails application. Tutorial creates a blog

application.

$ rails new <project name>

CS 390 Software Engineering - Lecture 7Septebmer 7, 2018 10

Page 11: Lecture 7 - Ruby on Railsuenics.evansville.edu/~hwang/f18-courses/cs390/Lecture07-Ruby-on-Rails.pdf · Ruby on Rails Rails is a web application development framework written in the

Rails webserver

Rails comes with its own webserver called Puma.

Start it (from a Rails project directory) using

$ bin/rails server

Puma needs a JavaScript runtime. Use Node.js, if

needed.

$ sudo apt install nodejs

Might also need to install the execjs gem

$ gem install execjs

Access it using URL http://localhost:3000

▪ Should show the Rails default page

CS 390 Software Engineering - Lecture 7Septebmer 7, 2018 11

Page 12: Lecture 7 - Ruby on Railsuenics.evansville.edu/~hwang/f18-courses/cs390/Lecture07-Ruby-on-Rails.pdf · Ruby on Rails Rails is a web application development framework written in the

Generating a controller and a view

To create a controller and a view, run the "controller"

generator and give the name of the controller and an

action name to associate with it. E.g., a controller called

"Welcome" with an action called "index".

$ bin/rails generate controller Welcome index

The important files to note are the controller in app/controllers/welcome_controller.rb and

the view in app/views/welcome/index.html.erb

Edit welcome.index.erb to contain

<h1>Hello, Rails!</h1>

CS 390 Software Engineering - Lecture 7Septebmer 7, 2018 12

Page 13: Lecture 7 - Ruby on Railsuenics.evansville.edu/~hwang/f18-courses/cs390/Lecture07-Ruby-on-Rails.pdf · Ruby on Rails Rails is a web application development framework written in the

Changing application homepage

Edit config/routes.rb to

Rails.application.routes.draw do

get 'welcome/index'

root 'welcome#index'

end

The get line was generated when the controller was

created and says to map requests to http://localhost:3000/welcome/index to the

welcome controller's index action.

The root line says to map requests to the root of the

application to the welcome controller's index action

CS 390 Software Engineering - Lecture 7Septebmer 7, 2018 13

Page 14: Lecture 7 - Ruby on Railsuenics.evansville.edu/~hwang/f18-courses/cs390/Lecture07-Ruby-on-Rails.pdf · Ruby on Rails Rails is a web application development framework written in the

Homework 1 Assignment

Restarting the Rails server (or refreshing the page)

should show the new application homepage.

Complete the rest of the tutorial to create a simple blog

application. Be prepared to demonstrate a working

application no later than Friday, September 14, for full

credit (20 points).

CS 390 Software Engineering - Lecture 7Septebmer 7, 2018 14