Introduction to information systems RUBY ON RAILS dr inż. Tomasz Pieciukiewicz.

Slides:



Advertisements
Similar presentations
PHP + Framework + MVC. What is Framework? Common code - Generic functionality Extensible - Specific functionality Unlike library – Flow Dictated by.
Advertisements

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 15 Introduction to Rails.
SharePoint Forms All you ever wanted to know about forms but were afraid to ask.
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.
Keys, Referential Integrity and PHP One to Many on the Web.
1.  Understanding about How to Working with Server Side Scripting using PHP Framework (CodeIgniter) 2.
PRACTICAL PHP AND MYSQL WALKTHROUGH USING SAMPLE CODES – MAX NG.
Creating Web Services with Ruby on Rails Robert Thew Internet and Web Systems II.
Ruby on Rails Model of MVC. Model-View-Controller Paradigm A way of organizing a software system Benefits: Isolation of business logic from the user interface.
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.
XML and Ruby on Rails Jennifer Andrews LIS 531F April 25,2007.
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.
Ruby on Rails Creating a Rails Application Carol E Wolf CS396X.
Ruby on Rails: An Introduction JA-SIG Summer Conference 2007 Michael Irion The University of Tulsa.
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:
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.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Cookies & Sessions.
CSCI 6962: Server-side Design and Programming Secure Web Programming.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
Why rails? Carlos Kirkconnell. Google Happiness leads to Productivity Happiness Matters.
FUNCTIONS AND STORED PROCEDURES & FUNCTIONS AND PROTECTING A DB AND PHP (Chapters 9, 15, 18)
MVC & ActiveRecord by Christian Mohr & Mohamed Souiai.
1 Dr Alexiei Dingli Web Science Stream Advanced ROR.
Standalone Java Application vs. Java Web Application
1 Dr Alexiei Dingli Web Science Stream Introducing Rails.
Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4.
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.
Introduction to Entity Framework Part 2 CRUD Scaffolding Tom Perkins NTPCUG.
Oracle Application Express Security. © 2009 Oracle Corporation Authentication Out-of-the-Box Pre-Configured Schemes LDAP Directory credentials Oracle.
PHP and MySQL CS How Web Site Architectures Work  User’s browser sends HTTP request.  The request may be a form where the action is to call PHP.
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.
© Copyright IBM Corporation 2007 AP/Americas April 15-18, 2007 Anaheim, California Introduction to RubyOnRails - a J2EE replacement? Russell Scheerer –
The Active Record Paradigm Databases in Database-Centric Web Site Development.
Photo Gallery INFO 2310: Topics in Web Design and Programming.
Creating PHPs to Insert, Update, and Delete Data CS 320.
Web Application Development Technology เทคโนโลยีสำหรับการพัฒนาโปรแกรม ประยุกต์เว็บ Suntorn Witosurapot Phone: or
Building Secure Web Applications With ASP.Net MVC.
XRX Basic CRUDS Create, Read, Update and Delete and Search XML Data Date: May 2011 Dan McCreary President Dan McCreary & Associates
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?
1 Active Records. 2 What’s Active Records? O-R Mapping layer To make database access almost a non-issue Relies heavily on convention over configuration.
Chapter 15 © 2013 by Pearson Overview of Rails - Rails is a development framework for Web-based applications - Based on MVC architecture for applications.
 Enhancing User Experience  Why it is important?  Discussing user experience one-by-one.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Introduction to Ruby&Rails Yuri Veremeyenko Monica Verma.
Chapter 12© copyright Janson Industries Java Server Faces ▮ Explain the JSF framework ▮ SDO (service data objects) ▮ Facelets ▮ Pagecode classes.
Rails and routing INFO 2310: Topics in Web Design and Programming.
Ruby on Rails & Databases. Active Record Active Record in Rails CRUD & Other Stuff Mapping Cardinalities Migrations Demo.
CS 160 and CMPE/SE 131 Software Engineering February 9 Class Meeting Department of Computer Science Department of Computer Engineering San José State University.
The Controller Carol Wolf Computer Science. Rails generate commands  Using the generate command, you can create a number of useful objects.  Rails:
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
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.
1 Rails for the Ruby-Impaired John Paul Ashenfelter CTO/Transitionpoint.
Advanced Migration By Aye Mon Tun.  To change database schema in consistent and easy way  In ruby code Migration? 11/25/2013 2Web Application Engineering.
BIT 286: Web Applications Lecture 04 : Thursday, January 15, 2015 ASP.Net MVC -
What is Laravel ? By Georgi Genov.
CS320 Web and Internet Programming SQL and MySQL
Play Framework: Introduction
Ruby on Rails Model of MVC
Ruby on Rails by Manik Juneja
ISC440: Web Programming 2 Server-side Scripting PHP 3
Ruby on Rails by Manik Juneja
Chapter 15 Introduction to Rails.
CS3220 Web and Internet Programming SQL and MySQL
Presentation transcript:

Introduction to information systems RUBY ON RAILS dr inż. Tomasz Pieciukiewicz

Rails–web development framework for Ruby  Introduction to Rails  View and Controller in Rails  Model in Rails  Scaffolding  Plugins

Intro

Convention over configuration  Use of default settings, configuration and coding standards results in shorter,easier code  Everything may be reconfigured– but why bother?

DRY  Don’t repeat yourself  If a programmer stated a piece of information somewhere, it doesn’t have to be repeated

Command line  Important operations are executed with command line - invoked scripts, e.g.:  rails new appName – generate app skeleton  rails server – run built-in www server  rails generate controller Name –create controller class  Gem –command line tool for package downloads (apt equivalent)  Rake – Ant equivalent

Important features  MVC based  built in ORM  many utility packages  seriously reduces development time

Controllers and views

Controller  Controller class extends the Application Controller class  Class name is NameController  Class is saved to name_controller.rb in the controllers folder  controllers folder may contain subfolders -> controller hierarchy

Controller  With default routing rules each method in the controller is an action URL contains method's name  n  parameters may be passed this way:  n/param1/param2

Controller  class HelloController < ApplicationController  def index  end  def there  end

View  View is a.erb.html file with the same name, as the action using it, put in the application folder, views\controller Name subfolder .erb.html is html with embedded Ruby code

View  Ruby code is put in tags  Expression values are in tags  Like in other similar languages, Ruby codemay be used to display HTML in certainways(loops, conditional expressions)  Code should be limited to view-related  processing, business logic goes into controller and/or model

View  View has access to instance attributes of invoking controller instance  This way, we may easily pass information from controller to view

View  By default, the controller's action renders the appropriate view at the end of its execution  render method may be used to display a different view:  render(:action => :actionName) –displays view related to a specific action  render(:file => path) –displays view from a specific file

View  hrefs to to other actions may be inserted using link_to method  "actionname"%>  "controllername", :action=>"actionname%>

View and controller  Access to request parameters –using the  params array:params[:paramName]  If a form element (e.g. select) may return multiple values, its name has to end with[]

View and controller  Session variables are stored in session association array:   Access to cookies using cookies association array:  cookies[:name]={:value=>value, :expires=when}

Models

Custom implementation  Located in the models folder  Each model class defines its own methodsand attributes, handles persistence  May extend e.g. the Base class (class that implements database access)

Active Record – typical approach  Create tables in database  Configure the database.yml file (config folder)  Generate model  rails generate model modelName

Database tables  Convention over configuration  Names in plural (english rules), e.g.:  Table bikes -> class Bike  Table people -> class Person  Primary key should be named id and be integer. Autoincrement is a good idea here ;)  Foreign keys – name of the foreign table in singular+"_id", eg person_id, bike_id,

Active Record classes  Simple Active Record class:  class Purchase < ActiveRecord::Base  end  This class provides access to all tuple's columns (read and write), searching etc.  We have to define relations with other tables and operations

Active Record  Record creation  Using new method to create object and save to save it to DB:  b = Book.new :title=>"sth", :author=>"auth"  b.save  Using create method (w/o save):  Book.create :title=>"sth",:author=>"auth"

Active Record  Record deletion  Database-level  delete(id)  delete 552  delete_all [condition]  Book.delete_all (['year<?',2007])  Object-level (recommended) - destroy  destroy_all [condition]  destroy_all ['alive=0']

Active Record  Updates  save – saves all changes to the database  CurrentBook.save  update_attribute – updates a specified attribute  CurrentBook.update_attribute :title=>'RoR'  update_attributes – updates a specified set of attributes  CurrentBook.update_attributes {:title=>'RoR',  :year=>2007}  update_all – update to a large group of records, change and conditions have to be specified  Book.update_all "price=1.22*price", :year=>2008

Active Record  Searching  within model, using SQL  find_by_sql "sql query"  User.find_by_sql "SELECT * FROM users"  count_by_sql "sql query"  User.count_by_sql "SELECT COUNT(*) FROM  users"

Active Record  Searching  outside model, using SQL  connection.select_all "sql query"  connection.select_one "sql query"  connection.execute "sql query" (does not have to  be search)

Active Record  Searching  using Active Record methods  Model.find id – using primary key as parameter  Book.find 548  Model.find :conditions=>['column=?',value]  Book.find :conditions => ['id=?',2]  Book.find :conditions => {:id=>2}  Model.find_by_xxx and Model.find_all_by_xxx –  created automatically for all columns, xxx is the name of  the column  Book.find_by_id 222  Book.find_by_id [1,345,1112]  Book.find_all_by_title "Bible"

Active Record  Relationships  belongs_to – n-side of 1:n relationship (n=1  or *)  has_one – 1-side of 1:1 relationship  has_many – 1-side of 1:* relationship  has_and_belongs_to_many – relationship *:*,  a table named table1_table2 must exist – will be used to estabilish *:* relationship

Active Record  Basic data validation  validates_presence_of :column - check if column contains data  guess:  validates_acceptance_of  validates_associated  validates_confirmation_of  validates_each  validates_exclusion_of  validates_format_of  validates_inclusion_of  validates_length_of  validates_numericality_of  validates_presence_of  validates_size_of  validates_uniqueness_of

Active Record  Data validation using methods:  class Comment < ActiveRecord::Base  validate :must_be_friends  def must_be_friends  errors.add_to_base("Must be friends \  to leave a comment") unless \  commenter.friend_of?(commentee)  end

Active Record  classBook < ActiveRecord::Base  has_and_belongs_to_many: authors  has_many:editions  validates_presence_of :title, :price  end

Active Record  SQL Injection protection  Never:  User.all :conditions => "login='#{login}' AND passwd='#{passwd}'"  Always:  User.all :conditions => ["login=? AND passwd=?", login, passwd]

Form helpers

form_for  form_for - used for HTML forms based upon ActiveRecord objects  <%=  :url => { :controller => "controller", :action => "action" },  :html => { :multipart => true, :method => :put }) do |f| %> ... 

form_for  :class - (required) name of model object for fields. Input fields will be prefixed with this - (optional) ActiveRecord model object, if named differently than class  :html - (optional) hash of HTML attributes for tag  :method - (optional) HTTP method to use  :url - url to post the form to  |f| - form object, used to create fields

Input field helpers  Multiple helpers, each used to create different type of form field  f.error_messages_for  f.check_box  f.file_field  f.hidden_field  f.label  f.password_field  f.radio_button  f.text_area  f.text_field  Description of parameters can be found here: 

More helpers  There are also helpers used to create select fields from collections:  pers/FormOptionsHelper.html create date and time input fields  pers/DateHelper.html and many others

Scaffolding

 Scaffolding allows us to quickly and easily create a simple CRUD application  It also generates Ruby code required to create a database following a certain specification  The DB schema may be migrated to a DB server by using the Rake tool

Scaffolding  Creating scaffolding:  rails generate scaffold ModelName column:type, column:type …  rails generate scaffold Movie title:string description:text one_sheet_url:string  Migrating database  rake db:migrate

Thank you for your attention QUESTIONS?