Chapter 2 Flashcards
when you create new app, what git steps do you need to take:
git init
git add -A
git commit -am “initialize repository”
connect new app to github:
create new repository on github manually.
then:
git remote add origin https://github.com/Saweel/toy_app.git
how to remove origin from git repo and set another one
git remote set-url origin newURL
data model
representation of the structures needed by our application. conceptual model of how data items relate to each other
data model for toy_app
microblog with users and short microposts.
data model for users:
unique integer identifier id, name, email
data model for microposts:
id and a content field for the text. also a user_id to associate each micropost with a particular user
User resource:
think of users as objects that can be created, read, updated, deleted through the web via the HTTP protocol (created using scaffoldin)
generate user with name and email using scaffolding
rails generate scaffold User name:string email:string
update the database in rails after creating a new user data model
bundle exec rake db:mirate
what is “make”?
unix, make utility is used to build executable programs from source code.
Rake:
rake is rubys make, a make-like language written in ruby. uses rake for little administrative tasks necessary developing database-backed web application.
bundle exec rake -T db
list of database tasks
bundle exec rake -T
all the rake tasks available
URL/users
index, page to list all users
URL/users/1
show, page to show user with id 1
URL/users/new
new, page to make new user
URL/users/1/edit
edit, page to edit user with id 1
what is the 1 mean in URL/users/1
the id attribute of the user
in terms of MVC, describe the results of a typical browser hit- a visit to the user index page at /users (BIG ANSWER)
- browser issues request for the /users URL
- Rails routes /users to the index action in the Users controller
- index action asks User model to retrieve all users
- User models pulls all the users from the database
- User model returns the list of users to the controller
- controller captures users in the @users variable, which is passed to the index view
- view uses embedded ruby to render the page as HTML
- controller passes the HTML back to the browser
routes.rb, explain “resources :users”
creates the mapping of user URLs to controller actions for the User resource.
add a root route for users
root ‘users#index’
rails: index,new,show,edit correspond to pages. describe create, update, and destroy actions
they dont typically render pages (although they can); main purpose is to modify information about users in the database
index,show,new,create,edit,update,destroy are actions in users_controller. describe the http request for each
index -> get show -> get new -> get create -> post edit -> get update -> patch destroy -> delete
REST define (simple)
REpresentational State Transer (rest). architectual style for developing distributed, networked systems and software applications.
what does REST mean in the context of rails
application components(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 database and to the four fundamental HTTP request methods: POST, GET, PATCH, DELETE.
@users
variables with @, are instance variables, and are automatically available in the views
iterates through each object stored in @users
weaknesses of this User resource
no data validations no authentication no tests no style or layout no real understanding
generate a micropost with a user id and content text
rails genrate scaffold Micropost content:text user_id:integer
validate micropost to accept 140 characters for the content
in micropost.rb:
validates :content, length: { maximum: 140 }
create associations between user having many microposts and microposts belonging to one user
user.rb: has_many :microposts ------------------------- micropost.rb: belongs_to :user
class User
User model inherits from ActiveRecord::Base, which is a base class for models provided by ActiveRecord (which lets us gain the ability to communicate with the database, treat database columns as ruby attributes, ect.)
class UsersController
User inherits from ApplicationController, which inherits from ActionController::Base (controllers gain large amounts of functionality by inhering from a base class, including ability to manipulate model objects, filter inbround HTTP requests, render views as HTML.)
heroku wont work if you just push it. what must you do so the app works correctly (hint: database)
you need to get the applications database to work, so you need to migrate the production database:
heroku run rake db:migrate
scaffolding strengths:
high level overview of rails introduction to mvc first taste of REST beginning data modelling live, database-backed web application in production
scaffolding weaknesses:
no custom layout no static pages (home or about) no user passwords no images no login no security no automatic user/micropost association no notion of following no micropost feed no meaninful tests NO REAL UNDERSTANDING