Rails What is Rails n n Rails is

  • Slides: 33
Download presentation
Rails

Rails

What is Rails? n n Rails is a framework for building web applications This

What is Rails? n n Rails is a framework for building web applications This involves: n n n 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 n n Rails applications can be built much more quickly Rails requires knowledge of fewer technologies

Required software n You need: n n n n The Ruby language A database,

Required software n You need: n n n n The Ruby language A database, such as My. SQL A Ruby-capable web server, such as lightppd, WEBRick, or Mongrel On Windows, Instant. Rails 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: n n n A good text editor, such as Text. Mate or Text. Pad A Ruby IDE, such as Eclipse with the RDT plugin, or Rad. Rails A GUI interface for looking at your database

Rails philosophy n Convention over configuration n Don't Repeat Yourself (DRY) n n n

Rails philosophy n Convention over configuration n Don't Repeat Yourself (DRY) n n n 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 n n n 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 n n By default, Rails projects use three environments The development environment

Three environments n n By default, Rails projects use three environments The development environment n n n The test environment n n n 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 n n n Classes are only loaded once Changes typically require stopping and restarting the server The environment is tuned for speed

Scripts n n A lot of work is done in the Rails world by

Scripts n n A lot of work is done in the Rails world by generate and rake scripts The generate scripts are called like this: ruby scriptgenerate generator options args n n n 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 n n 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 n n To create a new application, say, “My. Application”, enter

Starting an application n n To create a new application, say, “My. Application”, enter the command: rails My. Application This creates a directory (folder) named My. Application and, beneath it, a large directory structure and a number of files

Creating an empty database n The following instructions are for My. SQL on Windows,

Creating an empty database n The following instructions are for My. SQL on Windows, as configured in Instant. Rails 1. At the command line, enter mysql -u root –p and just hit Enter when prompted for a password 2. Type the following lines into My. SQL: • create database My. App_development; grant all on My. App_development. * to 'ODBC'@'localhost'; The above creates the development database for an application named My. App; you can repeat these two lines to create the test and production databases, if you like 3. Type exit

The directories, I n Rails creates the following subdirectories of the My. Application directory:

The directories, I n Rails creates the following subdirectories of the My. Application directory: n n n n 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 n More subdirectories n n n log/ -- log files produced

The directories, II n More subdirectories n n n 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 n The app/ subdirectory contains n The controllers/ subdirectory, which contains

The app subdirectory n The app/ subdirectory contains n The controllers/ subdirectory, which contains n n n The helpers/ subdirectory, which contains n n n The application_helper. rb file A table_helper. rb file for each table in your application The models/ subdirectory, which contains n n 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 n n The index. rhtml file action. rhtml files for most of the methods in your controllers

Naming conventions in Ruby requires the following naming conventions: n n n Ruby encourages

Naming conventions in Ruby requires the following naming conventions: n n n Ruby encourages the following naming conventions: n n n 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 Camel. Case; for example, Active. Record 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” n n n In other web application frameworks, you need many

“Convention over configuration” n n n 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) n n Rails uses the MVC design pattern In somewhat oversimplified terms,

Model-View-Controller (MVC) n n Rails uses the MVC design pattern In somewhat oversimplified terms, n n n The Model does all the “business logic, ” or computation The View displays results to the user The Controller ties it all together n n n 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 n The Model: n n The tables have lowercased,

Naming conventions in Rails, I n The Model: n n The tables have lowercased, underscored, pluralized names A class that describes a record in a table has a Camel. Case singular name A file containing the class has a lowercased, underscored, singular name The View: n n n 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 Camel. Case and singular The helper file is lowercased, underscored, and singular, and named app/helpers/application_helper. rb

Naming conventions in Rails, II n The Controller: n n n The URL is

Naming conventions in Rails, II n The Controller: n n n The URL is lowercased, underscored, and singular, and has the form http: //. . . app/controller/method/parameters The controller class is Camel. Case and singular The file is lowercased, underscored, and singular, and has the form app/controllers/table_controller. rhtml The controller’s methods are lowercased and underscored, and should typically be verbs This sounds like a lot of conventions to learn, but in reality Rails creates most of these for you

Typical flow of control n n n A request from the browser goes to

Typical flow of control n n n A request from the browser goes to a controller in app/controllers/ The controller talks to the model in app/models/ (initially, by means of a scaffold) The model sends requests to the database (in db/) The database returns information to the model The model manipulates the information and returns it to the controller The controller uses a. rhtml template in app/views/ to send a response to the browser

Scaffolding n You can use “scaffolds” to build models and controllers for your application

Scaffolding n You can use “scaffolds” to build models and controllers for your application n n A scaffold is an initial set of files which you will modify to do the work of your application The script ruby scriptgenerate scaffold model controller will create the files app/models/model. rb and app/controllers/controller_controller. rb, among others scaffold adds these methods: index, list, show, destroy, new, create, edit, update To learn more about scaffold, see Action. Controller: : Scaffolding: : Class. Methods

The browser call n n n The user talks to the application from a

The browser call n n n The user talks to the application from a browser A request from the browser goes to a method in a controller Make the browser request with n n n http: //host: port/Class. Name/method/parameters If method is omitted, the default is index method must be defined in class_name_controller. rb The parameters are often blank

The controller n A request from the browser goes to a method in a

The controller n A request from the browser goes to a method in a controller n n n Create the controller with ruby script/generate controller Class. Name (use instead of / on Windows) The controller will be app/controllers/class_name_controller. rb The controller extends Action. Controller: : Base Parameters sent to the URL (for example, by appending ? key=value pairs), are available by calling the params method If xyz is a key, its value can be accessed with either params['xyz'] or params[: xyz] A controller method must end by either: n n Passing control to another controller method, with redirect_to : action => "some_other_method", or Passing control to a view template, either n n Explicitly by ending with render : action => "some_template", or Implicitly, by not specifying a render action; this will render the template with the same name as the method

The model n n If you are going to use a database (most Rails

The model n n If you are going to use a database (most Rails applications do), create the database first Create the model with ruby script/generate model Class. Name The model extends Active. Record: : Base The easiest things to add to the model are validations n n Validations check whether a requested database entry or update is valid A number of validation methods are defined in Active. Record: : Validations: : Base

Some useful validations n n n validates_presence_of(*attr_names) Tests that the attributes are not blank

Some useful validations n n n validates_presence_of(*attr_names) Tests that the attributes are not blank validates_numericality_of(*attr_names) Tests that the values of the attributes are numeric validates_length_of(attr_name, restrictions) Tests that the attribute's value satisfies the size restrictions. Some restrictions are: : minimum=>number, : maximum=>number, : is=>number, : within=>range validates_uniqueness_of(*attr_names) Tests that the attribute values are unique validates_inclusion_of(attr_name, : in=>enumerable_object) Tests that the attribute's value is in the enumeration validates_each(*attrs) {|record, attr, value|. . . } Tests each attribute with the code in the block

The view(s) n n Views are written as HTML templates, in files with the.

The view(s) n n Views are written as HTML templates, in files with the. rhtml extension The RHTML file uses ERb, embedded Ruby: n n n Ruby statements (code to be executed, but not included in the HTML page) are enclosed in <% %> Ruby values, to be included in the resultant HTML, are enclosed in <%= %> Some available methods are: start_form_tag, submit_tag, render, link_to, and end_form_tag

Components of a view n A view may consist of three kinds of parts:

Components of a view n A view may consist of three kinds of parts: the main view, a layout, and some partials Layout Main view Partial

Constructing a view n n Here’s what happens when you browse to http: //localhost:

Constructing a view n n Here’s what happens when you browse to http: //localhost: 3000/application/noun/verb Rails calls the verb method in controllers/noun_controller. rb. 1. When the controller's verb method finishes, Rails looks for a template named views/noun/verb. rhtml 2. Next, Rails looks for a template “layout” file named views/layouts/noun. rhtml • If it can't find this file, it looks for a layout file named views/layouts/application. rhtml. 3. If no layout file is found, Rails uses the views/noun/verb. rhtml template “as is” 4. If a layout file is found, Rails inserts the verb. rhtml into the layout file, in the location marked <%= yield %>, and uses this combination to return to the user 5. Partials in the verb. rhtml file, indicated by <%= render : partial => 'form' %> , are filled in

Methods useful in the view n n n params Returns a list of parameters,

Methods useful in the view n n n params Returns a list of parameters, as given to the controller render(: action => partial) Renders the given partial template within the current template link_to(name, options = { }, html_options = nil) Returns the HTML for an <href> link. n name is the text to be displayed. options are a hash of values to be used in creating the URL. html_options are a hash of key/value pairs to be added to the <href> tag n n One useful Rails-specific pair is : confirm => ' question? ' h(string) Replaces &, ", <, and > with & , " , < , and > , respectively. (This is a Ruby, not Rails, method. ) form_tag, submit_tag, text_area_tag, radio_button_tag, etc. Rails methods for creating HTML tags (but you can write the tags directly). stylesheet_link_tag(name) Causes the HTML to use the stylesheet in public/stylesheets/name. css

Where methods come from n n n Controllers extend Action. Controller: : Base Models

Where methods come from n n n Controllers extend Action. Controller: : Base Models extend Active. Record: : Base All the relevant methods should be documented somewhere in http: //api. rubyonrails. com/ n n Modules (mixins) make this more complicated, because a class can “mix in” methods from a variety of modules n n Bad: The documentation isn’t complete (where is the params method? ) Bad: The documentation doesn’t say what version it’s for Good: The source code is immediately available For example, validates_numericality_of is in module Active. Record: : Validations: : Class. Methods Yes, Rails is easy, but only after you learn your way around in it

Adding tables to the database n Rails cannot create a database; that has to

Adding tables to the database n Rails cannot create a database; that has to be done in SQL n n You can create a file (say, create. sql) containing SQL commands to create some tables, then execute mysql My. App_development <create. sql n n My. SQL commands were given earlier in this talk This executes the SQL commands in the My. App_development database, which is (presumably) the development database for an application named My. App However, you can (mostly) avoid SQL and write the database directly in Ruby, by using migrations

Starting migrations n To fully connect Rails to the database takes four steps 1.

Starting migrations n To fully connect Rails to the database takes four steps 1. To translate the database into Ruby code, move to the application directory and enter rake db: schema: dump this creates a file schema. rb in the db directory 2. Generate the migration file db/001_baseline_schema. rb by executing the line ruby scriptgenerate migration Baseline. Schema 3. To tell Rails that this is the first version of the database, edit the schema. rb file and change the line Active. Record: : Schema. define() do to Active. Record: : Schema. define(: version => 1) do 4. Finally, enter the command rake db: migrate This creates a schema_info table in the database to tell SQL that this is the first version

Modifying the database n To make changes to the database, first run the script

Modifying the database n To make changes to the database, first run the script ruby scriptgenerate migration Some. Meaningful. Name n n n This creates a file named xxx_some_meaningful_name. rb, where xxx is the next version number The file will look like this: class Some. Meaningful. Name < Active. Record: : Migration def self. up end def self. down end Edit this file to make the changes n n The up method should make the changes, the down method should undo them To “migrate” these changes to the actual database, run the script rake db: migrate

Methods to use in up and down n add_column(table_name, column_name, type, options = {

Methods to use in up and down n add_column(table_name, column_name, type, options = { }) n Adds a new column to the named table n n n remove_column(table_name, column_name) n n Removes a column from the database rename_column(table, column, new_column_name) n n The type may be : primary_key, : string, : text, : integer, : float, : decimal, : datetime, : timestamp, : time, : date, : binary, or : boolean The options may include : limit, : default, : null, : precision, or : scale Renames a column Other changes are most easily made by removing a column and adding it again with a different type or options

Status n n This talk is based on a paper I’m writing, A Concise

Status n n This talk is based on a paper I’m writing, A Concise Guide to Rails, at http: //alliance. seas. upenn. edu/~cis 700/ruby/conciserails. html I’m still learning my way around Rails myself n n n I may have some things wrong I know I’m missing some important things I would very much like to hear your corrections and suggestions n A small amount of extra credit is available

The End

The End