Aaaaaa

Páginas: 9 (2088 palabras) Publicado: 6 de abril de 2011
Ruby On Rails – A Cheatsheet
blainekendall.com

Ruby On Rails Commands
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/generatemodel Modelname gem update rails rails application rake appdoc rake --tasks rake stats ruby script/server

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 ending with -%>will surpress the newline that follows use method h() to escape html & characters (prevent sql attacks, etc)

Creating links
“action_name” %>
Compiled from numerous sources by BlaineKendall.com Last updated 12/6/05

“action_name”, :id => product %> “action_name”, :id => product}, :confirm => “Are you sure?” %>

Database
3 databases configured in config/database.ymlapplication_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“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 … end

Compiled from numerous sources by BlaineKendall.com

Last updated 12/6/05

class LineItem rails automatically sets the variable @content_for_layout to the page-specific contentgenerated 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 ifyou 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 BlaineKendall.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 multipleprocesses 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 keywordprivate 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
:id => product is shorthand for :id => product.id methods ending in ! are destructive methods (like wiping outvalues, 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 BlaineKendall.com

Last updated 12/6/05

Hooks and Filters
before_create() after_create() before_destroy() restrict access to methods using filters before_filter...
Leer documento completo

Regístrate para leer el documento completo.

Estos documentos también te pueden resultar útiles

  • Aaaaaa
  • aaaaaa
  • Aaaaaa
  • aaaaaa
  • aaaaaa
  • Aaaaaa
  • aaaaaa
  • aaaaaa

Conviértase en miembro formal de Buenas Tareas

INSCRÍBETE - ES GRATIS