of 59/59
Ruby: The Foundation Rails: The Framework Conclusion Ruby on Rails An Overview J. Dew Department of Computer Science and Engineering University of South Carolina April 11, 2007 J. Dew Ruby on Rails

Ruby on Rails - Jose M. Vidaljmvidal.cse.sc.edu/webapps/spring07/rails.pdf · Ruby: The Foundation Rails: The Framework Conclusion Ruby on Rails An Overview J. Dew Department of Computer

  • View
    234

  • Download
    1

Embed Size (px)

Text of Ruby on Rails - Jose M. Vidaljmvidal.cse.sc.edu/webapps/spring07/rails.pdf · Ruby: The Foundation...

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Ruby on RailsAn Overview

    J. Dew

    Department of Computer Science and EngineeringUniversity of South Carolina

    April 11, 2007

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Language BasicsCompletely Object OrientedMethods, Classes and Modules

    Outline

    1 Ruby: The FoundationLanguage BasicsCompletely Object OrientedMethods, Classes and Modules

    2 Rails: The FrameworkModel-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    3 Conclusion

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Language BasicsCompletely Object OrientedMethods, Classes and Modules

    Ths Basics

    Ruby is . . .A dynamic, open source programming language with a focuson simplicity and productivity. It has an elegant syntax that isnatural to read and easy to write.

    http://ruby-lang.org/

    Examplesputs "Hello World"

    name = getsputs "Hello #{name}"

    hash = {:id => 42}hash.has_key? :id

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Language BasicsCompletely Object OrientedMethods, Classes and Modules

    Ths Basics

    Ruby is . . .A dynamic, open source programming language with a focuson simplicity and productivity. It has an elegant syntax that isnatural to read and easy to write.

    http://ruby-lang.org/

    Examplesputs "Hello World"

    name = getsputs "Hello #{name}"

    hash = {:id => 42}hash.has_key? :id

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Language BasicsCompletely Object OrientedMethods, Classes and Modules

    Ths Basics

    Ruby is . . .A dynamic, open source programming language with a focuson simplicity and productivity. It has an elegant syntax that isnatural to read and easy to write.

    http://ruby-lang.org/

    Examplesputs "Hello World"

    name = getsputs "Hello #{name}"

    hash = {:id => 42}hash.has_key? :id

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Language BasicsCompletely Object OrientedMethods, Classes and Modules

    Ths Basics

    Ruby is . . .A dynamic, open source programming language with a focuson simplicity and productivity. It has an elegant syntax that isnatural to read and easy to write.

    http://ruby-lang.org/

    Examplesputs "Hello World"

    name = getsputs "Hello #{name}"

    hash = {:id => 42}hash.has_key? :id

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Language BasicsCompletely Object OrientedMethods, Classes and Modules

    Conditionals and Looping

    Examples

    3.downto(0) do |count|unless count == 0 then

    print "#{count}.."else

    puts "Blastoff!"end

    end

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Language BasicsCompletely Object OrientedMethods, Classes and Modules

    Conditionals and Looping

    Examples

    3.downto(0) do |count|unless count == 0 then

    print "#{count}.."else

    puts "Blastoff!"end

    end

    Produces 3..2..1..Blastoff!

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Language BasicsCompletely Object OrientedMethods, Classes and Modules

    Outline

    1 Ruby: The FoundationLanguage BasicsCompletely Object OrientedMethods, Classes and Modules

    2 Rails: The FrameworkModel-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    3 Conclusion

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Language BasicsCompletely Object OrientedMethods, Classes and Modules

    Everythings an Object

    Examples42.methods.sort

    returns

    ["%", "&", "*", "**", "+", "[email protected]", "-", "[email protected]","/", "", "[]", "^", "__id__","__send__", "abs", "between?", "ceil", "chr","class", "clone", "coerce", "display", "div","divmod", "downto", "dup", "eql?", "equal?",..."type", "untaint", "upto", "zero?", "|", "~"]

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Language BasicsCompletely Object OrientedMethods, Classes and Modules

    Outline

    1 Ruby: The FoundationLanguage BasicsCompletely Object OrientedMethods, Classes and Modules

    2 Rails: The FrameworkModel-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    3 Conclusion

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Language BasicsCompletely Object OrientedMethods, Classes and Modules

    Methods

    Exampledef factorial(n)n == 1 ? 1 : n * factorial(n-1)

    end

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Language BasicsCompletely Object OrientedMethods, Classes and Modules

    Classes

    Exampleclass Client

    def Client.most_lucrative(clients)# class method

    end

    def paid_in_full!# instance method

    end

    end

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Language BasicsCompletely Object OrientedMethods, Classes and Modules

    Modules

    Example from Railsmodule ActiveRecord

    class Base

    def save# create or update a record

    end

    end

    end

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    Outline

    1 Ruby: The FoundationLanguage BasicsCompletely Object OrientedMethods, Classes and Modules

    2 Rails: The FrameworkModel-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    3 Conclusion

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    The Model

    ConceptInteracts directly with the RDBMSMaintains the state of the applicationEnforces any rules or validations related to the dataEncapsulated by ActiveRecord in Rails

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    The Model

    ConceptInteracts directly with the RDBMSMaintains the state of the applicationEnforces any rules or validations related to the dataEncapsulated by ActiveRecord in Rails

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    The Model

    ConceptInteracts directly with the RDBMSMaintains the state of the applicationEnforces any rules or validations related to the dataEncapsulated by ActiveRecord in Rails

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    The Model

    ConceptInteracts directly with the RDBMSMaintains the state of the applicationEnforces any rules or validations related to the dataEncapsulated by ActiveRecord in Rails

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    The View

    ConceptResponsible for generating the UIWhere the HTML, JS, CSS, etc residesMostly printing variables and simple loopsActionView in Rails

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    The View

    ConceptResponsible for generating the UIWhere the HTML, JS, CSS, etc residesMostly printing variables and simple loopsActionView in Rails

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    The View

    ConceptResponsible for generating the UIWhere the HTML, JS, CSS, etc residesMostly printing variables and simple loopsActionView in Rails

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    The View

    ConceptResponsible for generating the UIWhere the HTML, JS, CSS, etc residesMostly printing variables and simple loopsActionView in Rails

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    The Controller

    ConceptCoordinates between the model(s) and the view(s)Responds to user input and routes accordinglyMost of the application logic goes hereActionController in the Rails framework

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    The Controller

    ConceptCoordinates between the model(s) and the view(s)Responds to user input and routes accordinglyMost of the application logic goes hereActionController in the Rails framework

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    The Controller

    ConceptCoordinates between the model(s) and the view(s)Responds to user input and routes accordinglyMost of the application logic goes hereActionController in the Rails framework

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    The Controller

    ConceptCoordinates between the model(s) and the view(s)Responds to user input and routes accordinglyMost of the application logic goes hereActionController in the Rails framework

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    Outline

    1 Ruby: The FoundationLanguage BasicsCompletely Object OrientedMethods, Classes and Modules

    2 Rails: The FrameworkModel-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    3 Conclusion

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    Assumptions

    Rails assumes thatyou have a RDBMS backendyou dont mind following certain naming conventionsyou want to be database agnosticyou think SQL embedded in your code is ugly

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    Assumptions

    Rails assumes thatyou have a RDBMS backendyou dont mind following certain naming conventionsyou want to be database agnosticyou think SQL embedded in your code is ugly

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    Assumptions

    Rails assumes thatyou have a RDBMS backendyou dont mind following certain naming conventionsyou want to be database agnosticyou think SQL embedded in your code is ugly

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    Assumptions

    Rails assumes thatyou have a RDBMS backendyou dont mind following certain naming conventionsyou want to be database agnosticyou think SQL embedded in your code is ugly

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    The PHP Way

    PHP Code$dbh = mysql_connect("host","user","pwd");mysql_select_db(my_project);

    $id = $_GET[id];$sql = "SELECT * FROM users WHERE id=$id";$result = mysql_query($sql);$row = mysql_fetch_assoc($result);

    echo "Welcome, " . $row[first_name] . ".";

    mysql_free_result($result);mysql_close($link);

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    The Rails Way

    Rails Database Configdevelopment:adapter: mysqlhost: hostusername: userpassword: pwddatabase: my_project

    This would normally appear in config/database.yml.

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    The Rails Way (continued)

    Rails Controller [email protected] = User.find(@params[:id])

    Rails View CodeWelcome,

    In this case, the controller code would be found inapp/controllers/user_controller.rb and the modelcode would be found in app/models/user.rb.

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    Object/Relational Mapping

    What is Rails doing behind the scenes here?

    ConceptMaps database tables to classesMaps rows to objectsMaps columns to attributes

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    Object/Relational Mapping

    What is Rails doing behind the scenes here?

    ConceptMaps database tables to classesMaps rows to objectsMaps columns to attributes

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    Object/Relational Mapping

    What is Rails doing behind the scenes here?

    ConceptMaps database tables to classesMaps rows to objectsMaps columns to attributes

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    ORM Example

    ExampleAssume you have a table with all of your employees stored in atable called employees.

    Employee.find(:all) do |employee|if employee.last_name == "Dew"employee.salary = employee.salary * 2employee.save

    endend

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    Outline

    1 Ruby: The FoundationLanguage BasicsCompletely Object OrientedMethods, Classes and Modules

    2 Rails: The FrameworkModel-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    3 Conclusion

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    Model Naming

    ConceptDatabase names should be pluralClass names should be singular in mixed caseFilenames should be singular with underscores

    ExampleIf you want a model for book orders, the database name shouldbe book_orders, the class should be named BookOrder,and stored in the file book_order.rb.

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    Model Naming

    ConceptDatabase names should be pluralClass names should be singular in mixed caseFilenames should be singular with underscores

    ExampleIf you want a model for book orders, the database name shouldbe book_orders, the class should be named BookOrder,and stored in the file book_order.rb.

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    Model Naming

    ConceptDatabase names should be pluralClass names should be singular in mixed caseFilenames should be singular with underscores

    ExampleIf you want a model for book orders, the database name shouldbe book_orders, the class should be named BookOrder,and stored in the file book_order.rb.

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    Model Naming

    ConceptDatabase names should be pluralClass names should be singular in mixed caseFilenames should be singular with underscores

    ExampleIf you want a model for book orders, the database name shouldbe book_orders, the class should be named BookOrder,and stored in the file book_order.rb.

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    Why use Rails?

    BenefitsEasy division of labor between programmers anddesignersLess time spent writing configuration filesProgrammers and/or designers new to a project knowwhere to find all assets in the projectDoing AJAX requests are just as easy as notVery nice and comprehensive community support anddocumentationAllows you to create complete web applications in days,not months

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    Why use Rails?

    BenefitsEasy division of labor between programmers anddesignersLess time spent writing configuration filesProgrammers and/or designers new to a project knowwhere to find all assets in the projectDoing AJAX requests are just as easy as notVery nice and comprehensive community support anddocumentationAllows you to create complete web applications in days,not months

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    Why use Rails?

    BenefitsEasy division of labor between programmers anddesignersLess time spent writing configuration filesProgrammers and/or designers new to a project knowwhere to find all assets in the projectDoing AJAX requests are just as easy as notVery nice and comprehensive community support anddocumentationAllows you to create complete web applications in days,not months

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    Why use Rails?

    BenefitsEasy division of labor between programmers anddesignersLess time spent writing configuration filesProgrammers and/or designers new to a project knowwhere to find all assets in the projectDoing AJAX requests are just as easy as notVery nice and comprehensive community support anddocumentationAllows you to create complete web applications in days,not months

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    Why use Rails?

    BenefitsEasy division of labor between programmers anddesignersLess time spent writing configuration filesProgrammers and/or designers new to a project knowwhere to find all assets in the projectDoing AJAX requests are just as easy as notVery nice and comprehensive community support anddocumentationAllows you to create complete web applications in days,not months

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    Why use Rails?

    BenefitsEasy division of labor between programmers anddesignersLess time spent writing configuration filesProgrammers and/or designers new to a project knowwhere to find all assets in the projectDoing AJAX requests are just as easy as notVery nice and comprehensive community support anddocumentationAllows you to create complete web applications in days,not months

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    Why use Rails? (continued)

    BenefitsRails (and Ruby) are open-source softwareSince Ruby is an interpreted language, it can be movedfrom platform to platform with very minimal changesRapid prototyping allows you to show your customer aworking demo instead of static mockups at the designmeetingsProven to be reliably scaleable

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    Why use Rails? (continued)

    BenefitsRails (and Ruby) are open-source softwareSince Ruby is an interpreted language, it can be movedfrom platform to platform with very minimal changesRapid prototyping allows you to show your customer aworking demo instead of static mockups at the designmeetingsProven to be reliably scaleable

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    Why use Rails? (continued)

    BenefitsRails (and Ruby) are open-source softwareSince Ruby is an interpreted language, it can be movedfrom platform to platform with very minimal changesRapid prototyping allows you to show your customer aworking demo instead of static mockups at the designmeetingsProven to be reliably scaleable

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Model-View-Controller ArchitectureDatabase-centric ProgrammingConvention over Configuration

    Why use Rails? (continued)

    BenefitsRails (and Ruby) are open-source softwareSince Ruby is an interpreted language, it can be movedfrom platform to platform with very minimal changesRapid prototyping allows you to show your customer aworking demo instead of static mockups at the designmeetingsProven to be reliably scaleable

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Final Remarks

    Apologies

    I only began to scratch the surface of RubyMore information about Ruby can be found athttp://www.ruby-lang.org/

    I only began to scratch the surface of RailsMore information about Rails can be found athttp://rubyonrails.org/

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Final Remarks

    Apologies

    I only began to scratch the surface of RubyMore information about Ruby can be found athttp://www.ruby-lang.org/

    I only began to scratch the surface of RailsMore information about Rails can be found athttp://rubyonrails.org/

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Final Remarks

    Apologies

    I only began to scratch the surface of RubyMore information about Ruby can be found athttp://www.ruby-lang.org/

    I only began to scratch the surface of RailsMore information about Rails can be found athttp://rubyonrails.org/

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Final Remarks

    Apologies

    I only began to scratch the surface of RubyMore information about Ruby can be found athttp://www.ruby-lang.org/

    I only began to scratch the surface of RailsMore information about Rails can be found athttp://rubyonrails.org/

    J. Dew Ruby on Rails

  • Ruby: The FoundationRails: The Framework

    Conclusion

    Questions?

    J. Dew Ruby on Rails

    Ruby: The FoundationLanguage BasicsCompletely Object OrientedMethods, Classes and ModulesRails: The FrameworkModel-View-Controller ArchitectureDatabase-centric ProgrammingConvention over ConfigurationConclusion