Chapter 3 Flashcards
bundle update
update the gems to make sure the versions match
git mv README.rdoc README.md
changing the format from RDoc to Markdown using git
git commit -am
(-a) all changes, (-m) message
note*: make sure to commit everything before deploying or else you might get an error.
ok
generate a controller to handle static pages names “StaticPages” with a plan to make actions for a home page and help page
rails generate controller StaticPages home about
rails s
rails server shortcut
rails c
rails console shortcut
rails g
rails generate shortcut
bundle
bundle install shortcut
rake
rake test shortcut
when you add a branch, what is a good next step to do?
push to remote repository
git status
git add -A
git commit -m “add static pages controller”
git push -u origin static-pages
subsequent pushes can omit the arguments (git push)
why StaticPages generates static_pages_controller.rb
convention in rails to use snake case. (rails generator converts camelcase to snake using the underscore method)
you generated a controller; how do you undo it?
rails destroy controller StaticPages home help
undo rails generate model User name:string email:string
rails destroy model User
undo bundle exec rake db:migrate
bundle exec rake db:rollback
rollback all the way to beginning
bundle exec rake db:migrate VERSION=0
router
creates the correspondence between URLs and web pages
get ‘static_pages/home
maps requests for the URL/static_pages/home to the home action in the Static Pages controller. responds to GET requect
HTTP
hypertext transfer protocol. basic operations: GET, POST, PATCH, DELETE (operations between client computer and a server)client: chrome, firefox, server:apache nginx.
HTTP operations define:
GET used for reading data on the web (get a page).everytime you visit a page, your browser submits a GET request. POST request sent by browser wen you submit a form. in rails (POST typically used for creating things, also for updates). PATCH and DELETE: update and destroy things on the remote server.
GET request example
visit a site like google.com
POST request example
submit a registration form creates a new user on the remote site
classes in rails
used to organize functions
def home end
in ruby, this would do nothing. but since in inherits from ApplicationController, the behavior of this method is specific to rails. when visiting URL/static_pages/home, rails looks for static pages controller and executes the code in home action, then renders a view.
test-driven development define:
TTD, testing technique in which the programmer writes failing tests first, and then writes the application code to get the test to pass.
writing automated tests has 3 main benefits:
- protect against regressions, where a functioning feature stops working for some reason.
- allow code to be refactored
- test act as a client for the application code, helping determine its design and its interface
when to write test first or after writng the code:
when test is short
when desired behavior isnt clear, write application code first
security: write tests first
when a bug is found, write test to reproduce it and protect against regression, then write code to fix it
lean against writing test for code likely to change.
write tests before refactoring code,
integration tests:
tests functionality across models, views and controllers (chapter 7)
test “should get home” do
get :home
assert_response :success
end
says lets test the home page by issuing a GET request to the home action and them make sure we receive a success status code in response (verifies via assertion that result is a success)
execute rails test command
bundle exec rake test
assert_select
lets us test for presence of a particular HTML tag (sometimes called a selector, hence the name)
DRY
dont repeat yourself. remove repition in code (such as html code). becuase we have embedded ruby in our views, we can dry our code.
embedded ruby
embeds ruby into a text document. often used to embed ruby code in an html document, similar to asp, jsp, and php. (.html.erb Erb is the primary template system for including dynamic content in web pages)
provide(:title, “Home”);
indicates that rails should call the provite function and associate the string “Home” with the label :title
executes the code inside, while executes it and inserts the result into the template. yield to insert the title
application.html.erb
layout file allows us to factor out common structure.
in application.html.erb layout file
this code is responsible for inserting the contents of each page into the layout. (converts home.html.erb to HTML and inserts it).
prevents cross-site request forgery (CSRF), a type of malicious web attack