RailsWhat is Rails?Required softwareRails philosophyThree environmentsScriptsStarting an applicationCreating an empty databaseThe directories, IThe directories, IIThe app subdirectoryNaming conventions in Ruby“Convention over configuration”Model-View-Controller (MVC)Naming conventions in Rails, INaming conventions in Rails, IITypical flow of controlScaffoldingThe browser callThe controllerThe modelSome useful validationsThe view(s)Components of a viewConstructing a viewMethods useful in the viewWhere methods come fromAdding tables to the databaseStarting migrationsModifying the databaseMethods to use in up and downStatusThe EndJan 14, 2019RailsWhat is Rails?Rails is a framework for building web applicationsThis involves:Getting information from the user (client), using HTML formsDoing validationMaintaining session informationManaging a databaseDisplaying results to the userRails differs from similar frameworks in C++ or JavaRails applications can be built much more quicklyRails requires knowledge of fewer technologiesRequired softwareYou need:The Ruby languageA database, such as MySQLA Ruby-capable web server, such as lightppd, WEBRick, or MongrelOn Windows, InstantRails provides all of theseOn Macintosh, Locomotive provides theseOn Linux you probably have to assemble the pieces yourselfIt’s also helpful to have:A good text editor, such as TextMate or TextPadA Ruby IDE, such as Eclipse with the RDT plugin, or RadRailsA GUI interface for looking at your databaseRails philosophyConvention over configurationOther frameworks use several XML configuration files to specify where everything is, and how the parts are relatedRails assumes a standard configuration.Don't Repeat Yourself (DRY)Every piece of information (program or data) should be represented once and only onceRails provides a structure that encourages DRYAgile developmentIn Rails we start with a working program and “grow” it by making small changesIn Rails it is easy to re-test after every small changeRails provides strong support for unit testingThree environmentsBy default, Rails projects use three environmentsThe development environmentClasses are reloaded after every change, so every change you make happens “immediately”--you don’t have to restart the serverThis is where you do most of your workThe test environmentClasses are also reloaded after every changeFor each test, Rails creates a fresh copy of the data in the test databaseThe production environmentClasses are only loaded onceChanges typically require stopping and restarting the serverThe environment is tuned for speedScriptsA lot of work is done in the Rails world by generate and rake scriptsThe generate scripts are called like this:ruby script\generate generator options argsGenerators typically create files and directories for youSome built-in generators are: controller, model, scaffold, mailer, and web_serviceThe rake (like UNIX make) scripts are called like this:rake name:name:nameRake scripts are typically used to propagate changes by updating files that depend on other filesRake scripts are written in RubyStarting an applicationTo create a new application, say, “MyApplication”, enter the command: rails MyApplicationThis creates a directory (folder) named MyApplication and, beneath it, a large directory structure and a number of filesCreating an empty databaseThe following instructions are for MySQL on Windows, as configured in InstantRails1. At the command line, enter mysql -u root –pand just hit Enter when prompted for a password2. Type the following lines into MySQL: create database MyApp_development; grant all on MyApp_development.* to 'ODBC'@'localhost'; •The above creates the development database for an application named MyApp; you can repeat these two lines to create the test and production databases, if you like3. Type exitThe directories, IRails creates the following subdirectories of the MyApplication directory:app/ -- more about this directory latercomponents/ -- reusable componentsconfig/ -- configuration information, including database connection parametersdb/ -- database schema informationdoc/ -- autogenerated documentationlib/ -- code produced by your company and shared by many applicationsvendor/ -- purchased code shared by many applicationsThe directories, IIMore subdirectorieslog/ -- log files produced by the applicationpublic/ -- the web-accessible directory; your program appears to be running from hereRakefile/ -- scripts for creating documentation and testsscript/ -- utility scriptstests/ -- unit tests, functional tests, mocks, and fixturesOf these, the app subdirectory is the most important, but you will probably also use the config, db, and test directoriesThe app subdirectoryThe app/ subdirectory containsThe controllers/ subdirectory, which containsThe application.rb fileA table_controller.rb file for each table in your applicationThe helpers/ subdirectory, which containsThe application_helper.rb fileA table_helper.rb file for each table in your applicationThe models/ subdirectory, which containsA table.rb file for each table in your applicationThe views/ subdirectory, which containsThe index.rhtml fileaction.rhtml files for most of the methods in your controllersNaming conventions in RubyRuby requires the following naming conventions:The names of classes, modules, and constants must begin with a capital letterThe names of variables and methods must begin with a lowercase letterRuby encourages the following naming conventions:The names of classes, modules, and constants should be written in CamelCase; for example, ActiveRecordThe names of variables and methods should be written in all lowercase, with underscores between words; for example, chunky_baconFile names in Ruby follow the same rules as variable and function names; for example, my_application.rb“Convention over configuration”In other web application frameworks, you need many configuration files to specify how the various parts of your application fit togetherRails assumes you have named things in a certain way, then finds the parts and fits them together itselfRails uses both capitalization and pluralization rulesModel-View-Controller (MVC)Rails uses the MVC design patternIn
View Full Document