Resourse
start with ruby on rails
http://railsapps.github.io/installrubyonrails-mac.htmlchange ruby version
https://rvm.io/rubies/defaulttutorial: https://www.railstutorial.org/
Command Lines
rails _4.2.2_ new sample_app
bundle install
bundle install –without production
bundle update
heroku create
bundle exec rake db:migrate
bundle exec rake db:rollback
heroku run rake db:migrate
git push heroku master
rails console
rails generate scaffold …
rails generate model User name:string email:string
rails destroy model User
rails generate controller StaticPages home help
rails destroy controller StaticPages home help
bundle exec rake test (3)
bundle exec guard init (3.7.3)
bundle exec guard
ps aux | grep spring
Testing
Add this into
1 | ENV['RAILS_ENV'] ||= 'test' |
Git related
- git checkout master
- git checkout -b static-pages
- git checkout master
- git merge static-pages
Shortcut of Commands
Full command | Shortcut |
---|---|
$ rails server | $ rails s |
$ rails console | $ rails c |
$ rails generate | $ rails g |
$ bundle install | $ bundle |
$ rake test | $ rake |
Because Ruby uses CamelCase for class names (Section 4.4), my preference is to refer to controllers using their CamelCase names, but this is a matter of taste. (Since Ruby filenames typically use snake case, the Rails generator converts CamelCase to snake case using the underscore method.)
ruby
- Single-quoted strings are often useful because they are truly literal, and contain exactly the characters you type, like ‘\n’.
1 | "foobar" s = |
Since everything in Ruby is an object, it follows that nil is an object, so it too can respond to methods. One example is the to_s method that can convert virtually any object to a string:
1 | >> nil.to_s.empty? # Message chaining |
Blocks
1 | 1..5).each do |number| ( |
the map method returns the result of applying the given block to each element in the array or range.
1 | >> %w[A B C].map { |char| char.downcase } |
1 | >> ('a'..'z').to_a # An alphabet array |
Array and Hash
[] for Array, {} for Hash
1 | 42, 8, 17] a = [ |
Instead of defining hashes one item at a time using square brackets, it’s easy to use a literal representation with keys and values separated by =>, called a “hashrocket”:
1 | >> user = { "first_name" => "Michael", "last_name" => "Hartl" } |
Symbol
in Rails it is much more common to use symbols instead. Symbols look kind of like strings, but prefixed with a colon instead of surrounded by quotes. For example, :name is a symbol. You can think of symbols as basically strings without all the extra baggage
1 | >> user = { :name => "Michael Hartl", :email => "michael@example.com" } |
new syntax for defining hash
1 | >> h1 = { :name => "Michael Hartl", :email => "michael@example.com" } |
inspect
inspect returns a string with a literal representation of the object it’s called on
1 | 1..5).to_a.inspect # Put a literal array. puts ( |
class
1 | >> class Word < String # Word inherits from String. |
Integration Test
- rails generate integration_test site_layout
- bundle exec rake test:integration
- bundle exec rake test TEST=test/integration/users_login_test.rb
- bundle exec rake test TEST=test/integration/users_login_test.rb TESTOPTS=”–name test_login_with_valid_information”
Model
- rails generate model User name:string email:string
- rails generate migration add_remember_digest_to_users remember_digest:string
- rails generate migration add_admin_to_users admin:boolean
- bundle exec rake db:migrate
- rails generate migration add_activation_to_users activation_digest:string activated:boolean activated_at:datetime
- bundle exec rake db:rollback
- rails console –sandbox
- bundle exec rake test:models
- rails generate migration add_index_to_users_email
This uses a Rails method called add_index to add an index on the email column of the users table. The index by itself doesn’t enforce uniqueness, but the option unique: true does.
Sessions
- bundle exec rake routes
Avoid hijacking to Cookie
There are four main ways to steal cookies:
- using a packet sniffer to detect cookies being passed over insecure networks
- compromising a database containing remember tokens
- using cross-site scripting (XSS)
- gaining physical access to a machine with a logged-in user.
Rails preventes the first problem by using Secure Sockets Layer (SSL) site-wide, which protects network data from packet sniffers.
It prevents the second problem by storing a hash digest of the remember token instead of the token itself, in much the same way that we stored password digests instead of raw passwords.
Rails automatically prevents the third problem by escaping any content inserted into view templates.
Finally, although there’s no iron-clad way to stop attackers who have physical access to a logged-in computer, rails will minimize the fourth problem by changing tokens every time a user logs out and by taking care to cryptographically sign any potentially sensitive information we place on the browser.
Heroku
- heroku rename rails-tutorial-hello
Note that the application will briefly be in an invalid state after pushing but before the migration is finished. On a production site with significant traffic, it is a good idea to turn maintenance mode on before making the changes:
1 | $ heroku maintenance:on |