Ruby On Rails A Cheatsheet. Ruby On Rails Commands



Similar documents
Building Dynamic Web 2.0 Websites with Ruby on Rails

Deep in the CRUD LEVEL 1

Evaluation. Chapter 1: An Overview Of Ruby Rails. Copy. 6) Static Pages Within a Rails Application

Ruby on Rails is a web application framework written in Ruby, a dynamically typed programming language The amazing productivity claims of Rails is

6.170 Tutorial 3 - Ruby Basics

Rails Cookbook. Rob Orsini. O'REILLY 8 Beijing Cambridge Farnham Koln Paris Sebastopol Taipei Tokyo

Ruby on Rails on Minitest

Rails 4 Quickly. Bala Paranj.

1. Was ist das? II. Wie funktioniert das? III. Wo funktioniert das nicht?

CSCI110 Exercise 4: Database - MySQL

Drupal CMS for marketing sites

Portal User Guide. Customers. Version 1.1. May of 5

Introduction to Python

Designing and Implementing Forms 34

Visual COBOL ASP.NET Shopping Cart Demonstration

Building and Deploying Web Scale Social Networking Applications Using Ruby on Rails and Oracle. Kuassi Mensah Group Product Manager

Ruby On Rails. James Reynolds

Ruby on Rails Secure Coding Recommendations

PORTAL ADMINISTRATION

User s Guide. Version 2.1

Rake Task Management Essentials

Comparing Dynamic and Static Language Approaches to Web Frameworks

Web Application Vulnerability Testing with Nessus

PHP Tutorial From beginner to master

RESTful Rails Development. translated by Florian Görsdorf and Ed Ruder

Form And List. SuperUsers. Configuring Moderation & Feedback Management Setti. Troubleshooting: Feedback Doesn't Send

The NetBeans TM Ruby IDE: You Thought Rails Development Was Fun Before

Getting Started With Your Virtual Dedicated Server. Getting Started Guide

CommonSpot Content Server Version 6.2 Release Notes

Magento Extension Point of Sales User Manual Version 1.0

LabVIEW Internet Toolkit User Guide

ThirtySix Software WRITE ONCE. APPROVE ONCE. USE EVERYWHERE. SMARTDOCS SHAREPOINT CONFIGURATION GUIDE THIRTYSIX SOFTWARE

Learn how to create web enabled (browser) forms in InfoPath 2013 and publish them in SharePoint InfoPath 2013 Web Enabled (Browser) forms

Test Automation Integration with Test Management QAComplete

DbSchema Tutorial with Introduction in SQL Databases

Customization & Enhancement Guide. Table of Contents. Index Page. Using This Document

Developing Web Applications for Microsoft SQL Server Databases - What you need to know

WiredContact Enterprise x3. Admin Guide

Integrations. Help Documentation

Getting Started Guide. Getting Started With Your Dedicated Server. Setting up and hosting a domain on your Linux Dedicated Server using Plesk 8.0.

Accelerate Your Rails Site with Automatic Generation-Based Action Caching. Rod Cope, CTO and Founder OpenLogic, Inc.

A Beginner's Guide to Security with Ruby on Rails in BSD. Corey Benninger

Microsoft Access 3: Understanding and Creating Queries

Guides.rubyonrails.org

INTEGRATING MICROSOFT DYNAMICS CRM WITH SIMEGO DS3

User-password application scripting guide

Vodafone Hosted Services. Getting started. User guide

Database Forms and Reports Tutorial

Web Applications Testing

Other Language Types CMSC 330: Organization of Programming Languages

Integration Client Guide

FileMaker Security Guide The Key to Securing Your Apps

Volunteers for Salesforce Installation & Configuration Guide Version 3.76

PHP Integration Kit. Version User Guide

Pervasive Data Integrator. Oracle CRM On Demand Connector Guide

Commerce Services Documentation

Resources You can find more resources for Sync & Save at our support site:

NetSupport DNA Configuration of Microsoft SQL Server Express

GPMD CheckoutSuite for Magento Documentation

Ruby On Rails. CSCI 5449 Submitted by: Bhaskar Vaish

MASTERTAG DEVELOPER GUIDE

Sophos Mobile Control Administrator guide. Product version: 3

IBM BPM V8.5 Standard Consistent Document Managment

Parallels Plesk Panel 11 for your Linux server

Kentico CMS 7.0 E-commerce Guide

CMS Training Manual. A brief overview of your website s content management system (CMS) with screenshots. CMS Manual

Technical Support Set-up Procedure

Check list for web developers

Here is a quick diagram of the ULV SSO/Sync Application. Number 3 is what we deal with in this document.

Safewhere*Identify 3.4. Release Notes

Super Resellers // Getting Started Guide. Getting Started Guide. Super Resellers. AKJZNAzsqknsxxkjnsjx Getting Started Guide Page 1

How To Use Saml 2.0 Single Sign On With Qualysguard

latest Release 0.2.6

Module developer s tutorial

Bazaarvoice for Magento Extension Implementation Guide v6.3.4

An Introduction To The Web File Manager

1: 2: : 3.1: 3.2: 4: 5: & CAPTCHA

Application Developer Guide

Advanced Tornado TWENTYONE Advanced Tornado Accessing MySQL from Python LAB

Unit and Functional Testing for the ios Platform. Christopher M. Judd

CLC Server Command Line Tools USER MANUAL

Settings 1 September 2015

SyncTool for InterSystems Caché and Ensemble.

Ruby on Rails. Object Oriented Analysis & Design CSCI-5448 University of Colorado, Boulder. -Dheeraj Potlapally

Governance, Risk, and Compliance Controls Suite. Preventive Controls Governor Audit Rules User s Guide. Software Version

FileMaker Server 9. Custom Web Publishing with PHP

Tutorial: Building a Dojo Application using IBM Rational Application Developer Loan Payment Calculator

Facebook Twitter YouTube Google Plus Website

Advanced Web Development SCOPE OF WEB DEVELOPMENT INDUSTRY

An Introduction to Developing ez Publish Extensions

WebSphere Business Monitor V6.2 KPI history and prediction lab

Transcription:

Ruby On Rails A Cheatsheet blainekall.com Ruby On Rails Commands gem update rails rails application rake appdoc rake --tasks rake stats ruby script/server update rails create a new application generate documentation view available tasks view code statistics start ruby server at http://localhost:3000 ruby script/generate controller Controllername ruby script/generate controller Controllername action1 action2 ruby script/generate scaffold Model Controller ruby script/generate model Modelname URL Mapping http://www.example.com/controller/action Naming Class names are mixed case without breaks: MyBigClass, ThingGenerator Tablenames, variable names and symbols are lowercase with an underscore between words silly_count, temporary_holder_of_stuff ERb tags <%= %> <% %> ing with -%> will surpress the newline that follows use method h() to escape html & characters (prevent sql attacks, etc) Creating links <%= link_to Click me, :action => action_name %> Compiled from numerous sources by BlaineKall.com Last updated 12/6/05

<%= link_to Click me, :action => action_name, :id => product %> <%= link_to Click me, {:action => action_name, :id => product}, :confirm => Are you sure? %> Database 3 databases configured in config/database.yml application_development application_test application_production a model is automatically mapped to a database table whose name is the plural form of the model s class Database Table products orders users people Model Product Order User Person every table has the first row id it s best to save a copy of your database schema in db/create.sql Database Finds find (:all, :conditions => date available <= now() :order => date_available desc ) Relationships belongs_to :parent a Child class belongs_to a Parent class in the children class, parent_id row maps to parents.id class Order < ActiveRecord ::Base has_many :line_items Compiled from numerous sources by BlaineKall.com Last updated 12/6/05

class LineItem <ActiveRecord::Base belongs_to :order belongs_to :product notice the class LineItem is all one word, yet the database table is line_items and the reference to it in Order is has_many :line_items Validation validates_presence_of :fieldname1,fieldname2 is the field there? validates_numericality_of :fieldname is it a valid number? validates_uniqueness_of :fieldname is there already this value in the database? validates_format_of :fieldname matches against regular expression Templates if you create a template file in the app/views/layouts directory with the same name as a controller, all views rered by that controller will use that layout by default <%= stylesheet_link_tag mystylesheet, secondstyle, :media => all %> <%= @content_for_layout %> rails automatically sets the variable @content_for_layout to the page-specific content generated by the view invoked in the request Sessions Rails uses the cookie-based approach to session data. Users of Rails applications must have cookies enabled in their browsers. Any key/value pairs you store into this hash during the processing of a request will be available during subsequent request from the same browser. Rails stores session information in a file on the server. Be careful if you get so popular that you have to scale to multiple servers. The first request may be serviced by one server and the followup request could Compiled from numerous sources by BlaineKall.com Last updated 12/6/05

go to a secondary server. The user s session data could get lost. Make sure that session data is stored somewhere external to the application where it can be shared between multiple processes if needed. Worry about that once your Rails app gets really popular. if you need to remove your ruby session cookie on unix, look in /tmp for a file starting with ruby_sess and delete it. Request parameter information is held in a params object Permissions sections of classes can be divided into public (default), protected, private access (in that order). anything below the keyword private becomes a private method. protected methods can be called in the same instance and by other instances of the same class and its subclasses. attr_reader attr_writer attr_accessor attr_accessible # create reader only # create writer only # create reader and writer Misc <%= sprintf( %0.2f, product.price) %> :id => product is shorthand for :id => product.id methods ing in! are destructive methods (like wiping out values, etc destroy! or empty!) a built-in helper method number_to_currency will format strings for money Models add the line model :modelname to application.rb for database persisted models Compiled from numerous sources by BlaineKall.com Last updated 12/6/05

Hooks and Filters before_create() after_create() before_destroy() restrict access to methods using filters before_filter :methodname, :except => :methodname2 methodname must be executed for all methods except for methodname2. for example, make sure a user has been logged in before attempting any other actions Controllers /app/controllers/application.rb is a controller for the entire application. use this for global methods. different layouts can be applied to a controller by specifying: layout layoutname use the methods request.get? and request.post? to determine request type Helpers /app/helpers a helper is code that is automatically included into your views. a method named store_helper.rb will have methods available to views invoked by the store controller. you can also define helper methods in /app/helpers/application_helper.rb for methods available in all views module ApplicationHelper def some_method (arg) puts arg Views instead of duplicating code, we can use Rails components to redisplay it in other areas: <%= rer_component (:action => display_cart, :params =>{:context=> :checkout } ) %> Compiled from numerous sources by BlaineKall.com Last updated 12/6/05

the context causes it to set a parameter of context =>:checkout so we can control if the full layout is rered (which we don t want in this case) we change the method to include: def display_cart. if params[:context] == :checkout rer(:layout => false) param conditions can be used in the view as well: <% unless params[:context] == :checkout -%> <%= link_to Empty Cart, :action => empty_cart %> <% -%> Exception Handling usually 3 actions when an exception is thrown log to an internal log file (logger.error) output a short message to the user redisplay the original page to continue error reporting to the application is done to a structure called a flash. flash is a hash bucket to contain your message until the next request before being deleted automatically. access it with the @flash variable. begin. rescue Exception => exc logger.error( message for the log file #{exc.message} ) flash[:notice] = message here redirect_to(:action => index ) in the view or layout (.rhtml), you can add the following <% @flash[:notice] -%> <div id= notice ><%= @flash[:notice] %></div> <% -%> Compiled from numerous sources by BlaineKall.com Last updated 12/6/05

errors with validating or saving a model can also be displayed in a view(.rhtml) with this tag: <%= error_messages_for (:modelname) %> Forms <% start_form_tag :action => create %> <%= rer(:partial => form) %> #this will rer the file _form.rhtml <%= text_field ( modelname, modelparam, :size => 40) %> <%= text_area ( modelname, modelparam, rows => 4) %> <%= check_box ( fieldname, name.id, {}, yes, no} %> <%= options = [[ Yes, value_yes ],[ No, value_no ]] select ( modelname, modelparam, options) %> <%= submit_tag Do it %> <% _form_tag %> <%= check_box ( fieldname, name.id, {}, yes, no} %> results in <input name= fieldname[name.id] type= checkbox value= yes /> name.id should increment on multiple selection pages. {} is an empty set of options yes is the checked value no is the unchecked value a Hash (fieldname) will be created, name.id will be the key and the value will be yes/no Static value arrays in Models PAYMENT_TYPES = [ [ One, one ], [ Two, two ], [ Three, three ] ].freeze #freeze to make array constant Testing rake test_units run unit tests rake test_functional run functional tests rake run all tests (default task) ruby test/unit/modelname_test.rb run individual test Compiled from numerous sources by BlaineKall.com Last updated 12/6/05

ruby test/unit/modelname_test.rb n test_update run a single test method ruby test/unit/modelname_test.rb n /validate/ run all test methods containing validate (regex) rake recent run tests for files which have changed in the last 10 minutes Unit tests are for Models and functional tests are for Controllers. Functional tests don t need a web server or a network. Use the @request & @response variables to simulate a web server. rake clone_structure_to_test to duplicate development database into the test database(without the data) create test data using fixtures (example fest/fixtures/users.yml) user_george: id: 1 first_name: George last_name: Curious user_zookeeper: id: 2 first_name: Mr last_name: Zookeeper each key/value pair must be separated by a colon and indented with spaces, not tabs fixture can then be referenced in a test file as follows: fixtures :users, :employees fixtures can include ERb code for dynamic results: date_available: <%= 1.day.from_now.strftime ( %Y-%m-%d %H:%M:%S ) %> password: <%= Digest::SHA1.hexdigest( youcando ) %> create common objects in the setup() method so you don t have to recreate them repeatedly in each method. the teardown() method can also be used to clean up any tests. for example, if you have test methods which write to the database that aren t using fixture data, this will need to be cleared manually in teardown() def teardown TableName.delete_all Compiled from numerous sources by BlaineKall.com Last updated 12/6/05

test_helper.rb can be edited to create custom assertions. refactor repeating assertions into custom assertions. the first parameter is the result you expect, the second parameter is the actual result assert (boolean, message=nil) assert_equal (1, @user.id) assert_not_equal (expected, actual, message="") assert_instance_of (klass, object, message="") assert_kind_of (User, @user) assert_nil (object, message="") assert_not_nil (session[:user_id], message="user is empty") assert_throws (expected_symbol, message="") do... get :catch_monkey :id => @user_george.id post :login, :user => {:name => jim, :password => tuba } assert_response :success (or :redirect, :missing, :error, 200, 404) assert_redirected_to :controller => login, :action => methodname assert_template login/index assert_tag :tag => div a <div> node must be in the page assert_tag :tag => div, :attributes => {:class => errorsarea } assert_tag :content => Curious Monkeys assert_not_nil assigns[ items ] or assert_not_nil assigns(:items) assert_equal Danger!, flash[:notice] assert_equal 2, session[:cart].items assert_equal http://localhost/myapp, redirect_to_url follow_redirect() simulates the browser being redirected to a new page tests can call another test as well. if a previous test sets up data, that test can be used inside of other tests relying on that previous data. also of note is that the order of tests occuring within a testcase is not guaranteed. each test method is isolated from changes made by the other test methods in the same test case before every test method: 1) database rows are deleted 2) the fixture files are populated in the database Compiled from numerous sources by BlaineKall.com Last updated 12/6/05

3) the setup() method is run fixture data can be referenced directly in tests: assert_equal user_zookeeper[ id ], @user.id the named fixtures are automatically assigned an instance variable name. user_zookeeper can be referenced by @user_zookeeper the previous test becomes assert_equal @user_zookeeper.id, @user.id check the log/test.log file for additional debugging into the SQL statements being called. create mock objects when external depencies are required in testing. example, a mock object of models/cash_machine with only the external methods required to mock in it redefined: file /test/mocks/test/cash_machine.rb require models/cash_machine class CashMachine def make_payment(bill) :success #always returning a mock result gem install coverage install Ruby Coverage ruby rcoverage test/functional/my_controller_test.rb generates code coverage reports for performance and load testing, create performance fixtures in their own directory ( /test/fixtures/performance/orders.yml ) so they re not loaded for regular testing. create performance tests at /test/performance ruby script/profiler & ruby script/benchmarker can be run to detect performance issues Compiled from numerous sources by BlaineKall.com Last updated 12/6/05

Ruby Language local variables, method parameters, method names should all start with a lowercase letter or with an underscore: person, total_cost instance variables begin with @ use underscores to separate words in a multiword method or variable name such as long_document class names, module names and constants must start with an uppercase letter. WeatherMeter symbols look like :action and can be thought of as the thing named action to_s converts things to strings. to_i converts things to integers. to_a converts things to arrays. ruby comments start with a # character and run to the of the line two-character indentation is used in Ruby Modules are like classes but you cannot create objects based on modules. They mostly are used for sharing common code between classes by mixing into a class and the methods become available to that class. this is mostly used for implementing helper methods. In arrays the key is an integer. Hashes support any object as a key. Both grow as needed so you don t have to worry about setting sizes. It s more efficient to access array elements, but hashes provide more flexibility. Both can hold objects of differing types. a = [1, car, 3.14] a[0] a[2] = nil << apps a value to its receiver, typically an array a = [ ant, bee, cat, dog, elk ] can be written as: a = %w{ ant bee cat dog elk} # shorthand Compiled from numerous sources by BlaineKall.com Last updated 12/6/05

Ruby hashes uses braces and symbols are usually the keys basketball_team = { :guard => carter, :center => ewing, :coach => jackson } to return values: basketball_team [:guard] #=> carter Control Structures if count > 10... elsif tries ==3... else... while weight <190... unless condition else blocks use braces for single-line blogkcs and do/ for multiline blocks {puts Hello } both are blocks do club.enroll(person) person.drink case target-expr when comparison [, comparison]... [then] Compiled from numerous sources by BlaineKall.com Last updated 12/6/05

when comparison [, comparison]... [then]... [else ] until condition begin while condition begin until condition for name[, name]... in expr [do] expr.each do name[, name]... expr while condition expr until condition Interactive Ruby irb is a Ruby Shell, useful for trying out Ruby code % irb irb(main)> def sum(n1, n2) irb(main)> n1 + n2 irb(main)> => nil irb(main)> sum (3,4) => 7 irb(main)> sum ( cat, dog ) => catdog Compiled from numerous sources by BlaineKall.com Last updated 12/6/05

RDoc Documentation ri String.capitalize will show you the documentation for that method. ri String will show you the documentation for that class. you can access the Rails API documentation by running gem server and then going to http://localhost:8808 rake appdoc generates the HTML documentation for a project External Links wiki.rubyonrails.com search for login generator http://wiki.rubyonrails.com/rails/show/availablegenerators http://damagecontrol.codehaus.org - continuous builds What This Is I ve been studying Ruby On Rails over the past few weeks and taking notes as I go. This is a result of my amassed noteset from reading books, tutorials and websites. I also find writing notes helps you to remember so this was also a memorization exercise for myself. Document Versions 0.5 12/1/2005 First version. A list of notes from Agile Web Development with Rails, websites, tutorials, whytheluckystiff.net Compiled from numerous sources by BlaineKall.com Last updated 12/6/05