Intro to Rails INFO 2310: Topics in Web Design and Programming.

Slides:



Advertisements
Similar presentations
The Librarian Web Page Carol Wolf CS396X. Create new controller  To create a new controller that can manage more than just books, type ruby script/generate.
Advertisements

Chapter 15 © 2010 by Addison Wesley Longman, Inc Origins and Uses of Ruby - Designed by Yukihiro Matsumoto; released in Use spread rapidly.
Creating Web Services with Ruby on Rails Robert Thew Internet and Web Systems II.
Introduction to Model-View-Controller (MVC) Web Programming with TurboGears Leif Oppermann,
Ruby on Rails by Manik Juneja Ruby On Rails. Ruby on Rails by Manik Juneja Rails is a Web Application development framework. Based on the MVC pattern.
Server-Side vs. Client-Side Scripting Languages
ASP MVC s/mvc-4/getting-started-with- aspnet-mvc4/intro-to-aspnet- mvc-4.
Tutorial 6 Working with Web Forms
27-Jun-15 Rails. What is Rails? Rails is a framework for building web applications This involves: Getting information from the user (client), using HTML.
Tutorial 6 Working with Web Forms. XP Objectives Explore how Web forms interact with Web servers Create form elements Create field sets and legends Create.
A complete ror application. Some easy tutorials This uses oracle but it is easy to replace that with mysql:
Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.
MIS2502: Data Analytics MySQL and SQL Workbench David Schuff
Ruby on Rails CSE 190M, Spring 2009 Week 5. Installing Rails First, install Ruby with RubyGems Then, install the Rails gem gem install rails -version=2.3.2.
Ruby on Rails Creating a Rails Application Carol E Wolf CS396X.
INTRO TO MAKING A WEBSITE Mark Zhang.  HTML  CSS  Javascript  PHP  MySQL  …That’s a lot of stuff!
DR. MOHAMMAD IQBAL THANKS TO ADITYA SENGUPTA Comparing Web Frameworks.
Ruby on Rails (Slides modified by ements-2ed.shtml)
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
Ruby on Rails: An Introduction JA-SIG Summer Conference 2007 Michael Irion The University of Tulsa.
A little engineering on Rails Robert W. Hasker. Goals Intro to the Rails framework ▫Basic concepts: MVC, Active Record ▫A bit of Ruby Using Rails to build.
RUBY ON RAILS Mark Zhang. In this talk  Overview of Ruby on Rails  Core ideas  Show a tiny bit of example code  Touch on several general web development/
Ruby on Rails. What is Ruby on Rails? Ruby on Rails is an open source full-stack web framework. It is an alternative to PHP/MySQL. It can render templates,
Ruby on Rails CSCI 6314 David Gaspar Jennifer Garcia Avila.
Rails and Grails. To get started Make sure you have java installed You can get the sdk and jre at:
AJAX Chat Analysis and Design Rui Zhao CS SPG UCCS.
1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers.
UC Berkeley Hello Rails. Review: MVC Goal: separate organization of data (model) from UI & presentation (view) by introducing controller –mediates user.
Server-side Scripting Powering the webs favourite services.
BIT 286: Web Applications Lecture 04 : Thursday, January 15, 2015 ASP.Net MVC - Models.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
MVC & ActiveRecord by Christian Mohr & Mohamed Souiai.
1 Dr Alexiei Dingli Web Science Stream Advanced ROR.
Lecture 19 Web Application Frameworks Boriana Koleva Room: C54
1 Dr Alexiei Dingli Web Science Stream Introducing Rails.
Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for.
Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch February CSCE 740 Software Engineering.
Ruby on Rails CSE 190M, Spring 2009 Week 6. Overview How to use a database Demo creating a blog application on Rails Explain how the application works.
The Active Record Paradigm Databases in Database-Centric Web Site Development.
Associations INFO 2310: Topics in Web Design and Programming.
1 Dr Alexiei Dingli Web Science Stream A ROR Blog.
Photo Gallery INFO 2310: Topics in Web Design and Programming.
Ruby on Rails: Databases. Rails Database Familiar Table Concept Naming convention – lower case, plural (i.e. tweets) How to Access (find), Update, Delete.
Tutorial 6 Working with Web Forms. XP Objectives Explore how Web forms interact with Web servers Create form elements Create field sets and legends Create.
Tutorial 6 Working with Web Forms. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Explore how Web forms interact with.
Rails & Ajax Module 5. Introduction to Rails Overview of Rails Rails is Ruby based “A development framework for Web-based applications” Rails uses the.
RUBY ON RAILS (RoR) Ishwor Khadka. Why Ruby on Rails?
Chapter 15 © 2013 by Pearson Overview of Rails - Rails is a development framework for Web-based applications - Based on MVC architecture for applications.
Introduction to Ruby&Rails Yuri Veremeyenko Monica Verma.
Tutorial 6 Working with Web Forms. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Explore how Web forms interact with.
Date : 3/04/2010 Web Technology Solutions Class: PHP Web Application Frameworks.
Basic ActionScript and PHP Cis 126. Getting Started set up a basic folder structure so we can keep our files organized. Mirror this structure on your.
Rails and routing INFO 2310: Topics in Web Design and Programming.
CS 160 and CMPE/SE 131 Software Engineering February 9 Class Meeting Department of Computer Science Department of Computer Engineering San José State University.
Introduction to information systems RUBY ON RAILS dr inż. Tomasz Pieciukiewicz.
Introduction to Model-View-Controller (MVC) Web Programming with TurboGears Leif Oppermann,
Ruby on Rails. Web Framework for Ruby Designed to make it easier to develop, deploy, and maintain web applications Design with Model-View-Controller –almost.
MYSQL AND MYSQL WORKBENCH MIS2502 Data Analytics.
BIT 286: Web Applications ASP.Net MVC. Objectives Applied MVC overview Controllers Intro to Routing Views ‘Convention over configuration’ Layout files.
Presented by Alexey Vedishchev Developing Web-applications with Grails framework American University of Nigeria, 2016 Intro To MVC Architecture.
1 Adding a Model. We have created an MVC web app project Added a controller class. Added a view class. Next we will add some classes for managing movies.
Mid-Hudson Valley Linux Users Group Ruby on Rails Web Development is Fun Again MHVLUG Meeting Jan 7 th 2009 Sean Dague sean.dague.net.
CMPE/SE 131 Software Engineering September 1 Class Meeting Department of Computer Engineering San José State University Fall 2016 Instructor: Ron Mak
Bruce Scharlau, University of Aberdeen, 2017
Ruby on Rails by Manik Juneja
Ruby on Rails by Manik Juneja
Chapter 15 Introduction to Rails.
CMPE/SE 131 Software Engineering February 2 Class Meeting
Presentation transcript:

Intro to Rails INFO 2310: Topics in Web Design and Programming

ISSA General Meeting Mon 4:30 in Upson 205 The Information Science Student Association will have its first General Meeting this Monday (9/8) in Upson 205 at 4:30 Meet other people in the IS major and learn about where/how they spent their summer Get involved in planning social events Get information about jobs/internships Bring other IS students and friends who are interested in the major We will have food!

Today We’ll start our blog project We’ll talk a lot about Rails design and philosophy –Rails is a philosophy-heavy framework

Without further ado… Let’s just do it, shall we? In the spirit of doing things first, understanding them later… Pull up a command shell If you’re using a flash drive, change to flash drive ( F: or G: or whatever…) Enter ‘ rails –d sqlite3 blog ’

F:\>rails -d sqlite3 new_blog create create app/controllers create app/helpers create app/models create app/views/layouts create config/environments create config/initializers create db create doc create lib create lib/tasks create log create public/images create public/javascripts create public/stylesheets create script/performance create script/process... Rails creates lots of files and directories…

Now… cd blog ruby script/generate scaffold post title:string body:text rake db:migrate

Fire up the web server… Rails has its own built-in webserver (like Apache, for example). We’ll use it to look at our application. ruby script/server => Booting WEBrick... => Rails application started on => Ctrl-C to shutdown server; call with --help for options [ :46:58] INFO WEBrick [ :46:58] INFO ruby ( ) [i386-mswin32] [ :46:58] INFO WEBrick::HTTPServer#start: pid=4796 port=3000

Let’s take a look! The server is listening on port Start a web browser and navigate to

But I wanna see it DO something… Go to Go ahead and play around for a while. Look at the URLs as you enter/edit/delete posts. If you want, look at the underlying DB. Go to blog/db, type ‘ sqlite3 development.sqlite3 ’. Now you can ‘ SELECT * FROM posts; ’

What did we do? At a high level: rails –d sqlite3 blog –Create the blog project, using the SQLite3 database ruby script/generate scaffold post title:string body:text –For the blog project, create one ‘model’ called ‘post’ having two attributes, title (type string) and body (type text), and create the associated files (including instructions on creating associated DB).

rake db:migrate –Create the posts DB.

Object-Relational Model

Objects and DBs Object-oriented programming is a useful thing to be able to do. How can connect information in DBs with objects?

Object-Relational Mapping Each DB table (e.g. ‘posts’) has a corresponding class (e.g. ‘Post’). Each column of the table is an attribute of the class. Each row of the table can be fetched/saved as an instance of the class.

See it The Post class is defined in blog/app/models/post.rb. Take a look…

post.rb class Post < ActiveRecord::Base end Not much there, eh? Each table is a subclass of ActiveRecord, which has lots of methods we can call to access the table.

Try it Hopefully you added some posts to your blog. We can interact directly with our Rails application via irb. In your blog directory, type ‘ ruby script/console ’.

Create a new post directly from the console. mypost = Post.new mypost.title = “Another post!” mypost.body = “I made it at the console!” mypost.save Check that it is there in your webbrowser…

Or pass a hash as the initial input… mypost2 = Post.new(:title => “Yet another post!”, :body => “Isn’t this great?”) mypost2.save

You can also do DB lookups using the Post class. Post.find(idnum) Post.find(:first) allposts = Post.find(:all) Post.find(:first, :conditions => {:title => “Another post!”}) Post.find_by_title(“Another post!”)

In general, most of the easy stuff we want to do via SQL we can just do with method calls…

Rails philosophy

Lots of philosophical underpinnings to Rails. It will be easier to understand how it works if you know why it was made to work that way. (You can keep poking around while we discuss this; maybe start looking through all the files that were created).

Agile software How does one usually design a website for a client?

Gather client requirements Document and design system Implement and test system Show system to client Repeat

Agile Manifesto Value: Individuals and interactions over processes and tools Working software over comprehensive documentation Customer collaboration over contract negotiation Responding to change over following a plan

Agility and Rails It’s easy to make small modifications and get them working. Then you can more easily work by making incremental changes, getting client feedback, and responding to it.

Don’t repeat yourself (DRY) Define/do something only in one place. “If you’re doing it twice, you’re doing it wrong!” Examples: Username/password/database name info to connect to a database – in one place, not multiple places in your code –Look this up in blog/config/database.yml.

database.yml # SQLite version 3.x # gem install sqlite3-ruby (not necessary on OS X Leopard) development: adapter: sqlite3 database: db/development.sqlite3 timeout: 5000 # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: adapter: sqlite3 database: db/test.sqlite3 timeout: 5000 production: adapter: sqlite3 database: db/production.sqlite3 timeout: 5000

DRY Making DB changes, such as adding attributes to a table. Write one script (a “migration”). Rake then automatically generates the correct SQL to make the change, makes it, and alters the object definitions in your code.

Convention over configuration Only define unconventional configurations. For everything else, just follow the defaults (‘conventions’) and things should just work. E.g. we’ll soon create another DB table called ‘comments’. We’ll be able to tell Rails that any Post has_many :comments Rails will automatically assume that the comments table has a column ‘post_id’ that is a foreign key to the posts table; i.e. you can then easily do joins. You can override things as needed. But if you follow the conventions, you write less code.

Model-View-Controller Pattern

Model-View-Controller Model-View-Controller (MVC) is a (now) standard architecture in software engineering. It separates the data and operations on the data from code that actually displays it. Separation means that we can make changes to one part without affecting what happens to the other.

PHP Things tend to get mixed up in the PHP applications we wrote in 230. <?php if (isset($_GET[‘sort’])) { $query = “SELECT * FROM Movies ORDER BY “.$_GET[‘sort’]; } else $query = “SELECT * FROM Movies”; $results = mysql_query($query,$link); while ($row = $mysql_fetch_array($results, MYSQL_ASSOC)) { print(“ ”); foreach ($row as $item) { print (“ $item ”); } print(“ ”); } ?>

Model The objects used to store and manipulate the data of the applications. We’ve already seen an example with the ‘Post’ class.

Controller Take requests (generated by the user interface) and decide which of the views to render.

View HTML (plus some Ruby) for displaying data from the models and getting user input from forms.

The cycle User interacts with the browser and triggers an event (e.g. submits a post) Controller receives input from the interface (e.g. the submitted post) Controller accesses the model, updating it in some way (e.g. creating a new post) The controller renders an updated view (e.g. shows the new post) The browser waits for new input from the user.

Controller code We’ve seen the ‘Post’ code; where are the views and controllers? Controller for ‘posts’ is in blog/app/controllers/posts_co ntroller.rb. Take a look…

class PostsController < ApplicationController # GET /posts # GET /posts.xml def = Post.find(:all) respond_to do |format| format.html # index.html.erb format.xml { render :xml } end # GET /posts/1 # GET /posts/1.xml def = Post.find(params[:id]) respond_to do |format| format.html # show.html.erb format.xml { render :xml } end

Roughly – what to do for each possible action on a post: Create (new) Read (show) Update (update) Delete (destroy) sometimes abbreviated as CRUD. Routes the URLs we saw in the application: posts/1, posts/1/edit… Decides which view to send to browser.

Views HTML in two locations: blog/app/views/layout/posts.html.erb blog/app/views/posts/index.html.erb, …/show.html.erb …/edit.html.erb …/new.html.erb

posts.html.erb <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " transitional.dtd"> Posts:

index.html.erb Listing posts Title Body 'Are you sure?', :method => :delete %>

PHP vs. Rails PHP: Only current PHP file (and any ‘required’ files) is processed Rails: Your entire application is loaded at once.

Learning More

Documentation Can learn more about ActiveRecord at verecord/README.html verecord/README.html Also ActionPack class has ActionView class and ActionController. ActionPack documentation at onpack/README.html. onpack/README.html

To do Try to improve the very basic styling of the post pages. CSS file is in public/stylesheets/scaffold.css.

ISSA General Meeting Mon 4:30 in Upson 205 The Information Science Student Association will have its first General Meeting this Monday (9/8) in Upson 205 at 4:30 Meet other people in the IS major and learn about where/how they spent their summer Get involved in planning social events Get information about jobs/internships Bring other IS students and friends who are interested in the major We will have food!