Note on 'Ruby on Rails'

Ruby on Rails


# .gemrc

install: --no-rdoc --no-ri

update: --no-rdoc --no-ri


$ gem install rails -v 3.2.3


$ rails generate scaffold User name:string email:string

// The name of the scaffold follows the convention of models, which are singular, rather than resources and controllers, which are plural. 

// There is no need to include a parameter for id; it is created automatically by Rails for use as the primary key in the database. 

// In order to ensure that the command uses the version of Rake corresponding to our Gemfile, we need to run rake using bundle exec. 


// you can see a list of database tasks using -T db

$ bundle exec rake -T db


// To see all the Rake tasks available, run

$ bundle exec rake -T


// REST - Representational State Transfer

// REST is an architectural style for developing distributed, networked systems and software applications such as the WWW and web applications. Although REST theory is rather abstract, in the context of Rails applications REST means that most application components(such as users and microposts) are modeled as resources that can be created, read, updated, and deleted --- operations that correspond both to the CRUD operations of relational databases and the four fundamental HTTP request methods: POST, GET, PUT, DELETE. 


// Heroku

$ git clone git://github.com/heroku/ruby-sample.git

$ cd ruby-sample

$ heroku create // --stack cedar 

$ git push heroku master

$ heroku open 



$ cd rails_projects

$ rails new sample_app --skip-test-unit

$ cd sample_app


// To configure Rails to use RSpec in place of Test::Unit. 

$ rails generate rspec:install


The model is responsible for maintaining the state of the application. The model acts as both a gatekeeper and a data store. 

The view is responsible for generating a user interface, normally based on data in the model. 

The view's work is done one the data is displayed. 

The controller orchestrate the application. Controllers receive events from the outside world, interact with model, and display an appropriate view to the user. 


Active Record is the solid model foundation of the Rails MVC architecture.

Action Pack: support for views and controllers 


Rails uses symbols to identify things. In particular, it uses them as keys when naming method parameters and looking things up in hashes.

redirect_to :action => "edit", :id => params[:id]

You can think of symbols as string literals that are magically made into constants. Alternatively, you can consider the colon to mean "the thing named", so :id is "the thing named id".


In the single-quoted case, Ruby does very little. With a few exceptions, what you type into the single-quoted string literal becomes the string's value.

'a', 'a\n', '#{fda}'

In the double-quoted case, Ruby does more work. 

"a\n", "#{fds}"



Difference between 'string' and "string":

> a = "abc"

> %q{abc#{a}} => "abc#{a}"

> %Q{abc#{a}} => "abcabc"


%w(ant bee cat dog elk) => ["ant", "bee", "cat", "dog", "elk"]


/pattern/ or %r{pattern}

\d => matches any digit

\s => matches any whitespace character

\w  => matches any alphanumeric ("word") character.



class Order < ActiveRecord::Base

    has_many :line_items

    

    def self.find_all_unpaid

self.where('paid = 0')

    end


    def total

        sum = 0

        line_items.each { |li| sum += li.total } 

        sum

    end

end


Here has_many is a method that's defined by Active Record. It's called as the Order class is being defined. 


Modules are similar to classes in that they hold a collection of methods, constants, and other module and class definitions. 

Modules serve two purposes.First, they act as a namespace, letting you define methods whose names will no clash with those defined elsewhere. Second, they allow you to share functionality between classes. 


Ruby can take an object and convert it into a stream of bytes that can be stored outside the application. The process is call marshaling. 

Rails uses marshaling to store session data. 


class CreateProducts < ActiveRecord::Migration

    def change

        create_table :products do |t|

            t.string :title

            t.text :description

            t.string :image_url

            t.decimal :price, precision: 8, scale: 2


            t.timestamps

        end

    end

end

This block, when called, is passed an object named t, which is used to accumulate a list of fields. Rails defines a number of methods on this object --- methods with names that named after common data types. These methods, when called, simply add a field definition to the ever-accumulating set of names. 



class Person < ActiveRecored::Base

    def self.for_dave

        Person.new(name: 'Dave')

    end

end


class Employee < Person

end


$ rails generate controller Store index


$ rake db:rollback

$ rake db:migrate


$ rake test

$ rake test:units


$ git checkout .


validates :title, :description, :image_url, presence:true

validates :price, numerically: { greater_than_or_equal_to: 0.01 } 

validates :title, uniqueness: true

validates :image_url, allow_blank: true, format {

    with: /\.(gif|jpg|png)$/i,

    message: 'must be a URL for GIF, JPG or PNG image.'

}


你可能感兴趣的:(Ruby,Rails)