Rails Jan 14 2019 What is Rails Rails is a framework for building web applications This involves Getting information from the user client using HTML forms Doing validation Maintaining session information Managing a database Displaying results to the user Rails differs from similar frameworks in C or Java Rails applications can be built much more quickly Rails requires knowledge of fewer technologies Required software You need The Ruby language A database such as MySQL A Ruby capable web server such as lightppd WEBRick or Mongrel On Windows InstantRails provides all of these On Macintosh Locomotive provides these On Linux you probably have to assemble the pieces yourself It s also helpful to have A good text editor such as TextMate or TextPad A Ruby IDE such as Eclipse with the RDT plugin or RadRails A GUI interface for looking at your database Rails philosophy Convention over configuration Don t Repeat Yourself DRY Other frameworks use several XML configuration files to specify where everything is and how the parts are related Rails assumes a standard configuration Every piece of information program or data should be represented once and only once Rails provides a structure that encourages DRY Agile development In Rails we start with a working program and grow it by making small changes In Rails it is easy to re test after every small change Rails provides strong support for unit testing Three environments By default Rails projects use three environments The development environment The test environment Classes are reloaded after every change so every change you make happens immediately you don t have to restart the server This is where you do most of your work Classes are also reloaded after every change For each test Rails creates a fresh copy of the data in the test database The production environment Classes are only loaded once Changes typically require stopping and restarting the server The environment is tuned for speed Scripts A lot of work is done in the Rails world by generate and rake scripts The generate scripts are called like this ruby script generate generator options args Generators typically create files and directories for you Some built in generators are controller model scaffold mailer and web service The rake like UNIX make scripts are called like this rake name name name Rake scripts are typically used to propagate changes by updating files that depend on other files Rake scripts are written in Ruby Starting an application To create a new application say MyApplication enter the command rails MyApplication This creates a directory folder named MyApplication and beneath it a large directory structure and a number of files Creating an empty database The following instructions are for MySQL on Windows as configured in InstantRails 1 At the command line enter mysql u root p 2 and just hit Enter when prompted for a password 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 like 3 Type exit The directories I Rails creates the following subdirectories of the MyApplication directory app more about this directory later components reusable components config configuration information including database connection parameters db database schema information doc autogenerated documentation lib code produced by your company and shared by many applications vendor purchased code shared by many applications The directories II More subdirectories log log files produced by the application public the web accessible directory your program appears to be running from here Rakefile scripts for creating documentation and tests script utility scripts tests unit tests functional tests mocks and fixtures Of these the app subdirectory is the most important but you will probably also use the config db and test directories The app subdirectory The app subdirectory contains The controllers subdirectory which contains The helpers subdirectory which contains The application helper rb file A table helper rb file for each table in your application The models subdirectory which contains The application rb file A table controller rb file for each table in your application A table rb file for each table in your application The views subdirectory which contains The index rhtml file action rhtml files for most of the methods in your controllers Naming conventions in Ruby Ruby requires the following naming conventions Ruby encourages the following naming conventions The names of classes modules and constants must begin with a capital letter The names of variables and methods must begin with a lowercase letter The names of classes modules and constants should be written in CamelCase for example ActiveRecord The names of variables and methods should be written in all lowercase with underscores between words for example chunky bacon File 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 together Rails assumes you have named things in a certain way then finds the parts and fits them together itself Rails uses both capitalization and pluralization rules Model View Controller MVC Rails uses the MVC design pattern In somewhat oversimplified terms The Model does all the business logic or computation The View displays results to the user The Controller ties it all together It gets input from the user It takes actions depending on what the user wants done this typically involves asking the Model to do some work then collecting the results of that work It tells the View to display those results Naming conventions in Rails I The Model The tables have lowercased underscored pluralized names A class that describes a record in a table has a CamelCase singular name A file containing the class has a lowercased underscored singular name The View The URL is lowercased underscored and singular and has the form http app controller method parameters The file is lowercased underscored and singular and has the form app views controller method rhtml The layout files in app views layout follow the same file naming conventions The helper module is CamelCase and singular The helper file is lowercased
View Full Document