Chapter 3 Flashcards
where are rails actions?
in the controller
switch to branch “static-pages”
git checkout -b static-pages
how can we generate a controller?
rails generate command to generate scaffolding
steps to make a controller to handle static pages
create StaticPages controller using generate. plan to make actions for home page, help page and about page.
$ rails generate controller StaticPages home help
$ rails generate conroller StaticPages home help
generates StaticPages controller with actions for home and help
scaffolding
generate major pieces of an application to create models, views, and controllers for a new resource
rails g
rails generate shortcut
rails c
rails console shortcut
bundle install
bundle shortcut
rake test
rake shortcut
git push -u origin static-pages
pushes topic branch up to github
destroy controller StaticPages:
rails destroy controller StaticPages home help
generate a model using scafffolding
rails generate model User name:string email:string
undo model generated User
rails destroy model User
bundle exec rake db:migrate
change the state of the database
undo db:migrate
bundle exec rake db:rollback (undo a single migration)
route file
responsible for implementing the router(defining the correspondence between URLs and web pages).
get ‘static_pages/home’
located in cofig: maps requests for the URL /static_pages/home to the home action in the Static Pages controller.
get ‘static_pages/home’ what is get
using get we arrange the route to respond to a GET request, HTTP
http
basic operations get, post, patch, and delete. operations between a client(chrome firefox) and a server(apache or nginx). emphasis on http verbs is typical of web frameworks influenced by the rest architecture.
get method
reading data on the web. just means ‘get a page’. everytime you visit google your browser submits a get request
post method
request sent by your browser when you submit a form. in rails, post are typically used for creating things(http also allows post to perform updates.
patch method
updating things on remote server
delete mthod
destroy things on remote server
classes in rails
organize functions like home and help action which are defined using def keyword
def home end in ruby this method is empty. is that true for Rails?
because it inherits ApplicationController the behavior of its methods is specific to rails. when visitin th url, rails executes the code inthe method AND renders the view. if method is empty, all it does is render the view.
TDD
test-driven development, programmers writes failing tests first, and then writes the application code to get the tests to pass
benefits of testing
- protects against regressions where a functioning feature stops working for some reason
- test allow code to be refactored(changing its form without changing its function)
- tests act as a client for the application code helping determine its design and its interface
when to test first(or test at all)
- if test is short compared to code, write test first
- when desired behavior of app isnt clear, write application code first.
- because security is a top priority, write test for security model first
- whenever a bug is found, write a test to reproduce it and protect against regressions, then write the code to fix it.
- against writing test code for html structure
- write tests before refactoring code
–usually write controller and model tests first and integration tests(functionality) second
test “should get home” do
get :home
assert_response :success
end
lets test the home page by using get request to the home action and then making sure we receive a success status code in response
run test suite to verify tests currently pass
bundle exec rake test
ActionController::UrlGenerationError:
No route matches {:action=>”about”, :controller=>”static_pages”}
no route matches the desired action/controller combination which is a hind that we need to add a line to the routes file
AbstractController::ActionNotFound:
The action ‘about’ could not be found for StaticPagesController
missing about action in static pages controller, which we can add
ActionView::MissingTemplate: Missing template static_pages/about
indicates a missing template, which in the context of rails is essentially the same thing as a view.
touch app/views/static_pages/about.html.erb
although touch is designed to update the modification of a file or directory without otherwise affecting it, as a die-effect it creates a new file if one doesnt exist
assert_select “title”, “Home | Ruby on Rails Tutorial Sample App”
checks for the presence of a tag containing the string.
DRY
Dont Repeat Yourself principle: our html paes contained almost identical titles, common parts to all three, and an entire HTML skeleton structure is repeated on each page. we’ll DRY out our code by removing repitition
ERb
embedded ruby, primary template system for including dynamic content in web pages
rails function provide fr titles
special rails function to set different titles on each page
indicates using that rails should call the provide function and associate the string “Home” with the label :title, then we use yield funtion in ruby to insert the title into the template
Ruby on Rails Tutorial Sample App
insert title label string into parts of html code
responsible for inserting the contents of each page into the layout. converts the contents of home.html.erb to HTML and then inserts it in place of
csrf_meta_tags
prevents cross-site request forgery (CSRF), a type of malicious web attack
purpose of rails layout
allow the use of a common template for pages in our application, thereby eliminating duplication