Lecture 7 - Ruby on...
Transcript of Lecture 7 - Ruby on...
CS 390 Software Engineering
Lecture 7 – Ruby on Rails
References: Getting Started With Rails, available at https://guides.rubyonrails.org/getting_started.html
Outline
Finish Ruby language basics
Ruby on Rails
CS 390 Software Engineering - Lecture 7Septebmer 7, 2018 2
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
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
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
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
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
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
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
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
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
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
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
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