Chapter 2 Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

create new rails app(all of its directories generated)

A

cd workspace
rails 4.2.2 new toy_app
cd toy_app

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

$ bundle install –without production

A

flag prevents local installation of any production gems

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what must you do after you made changes to the gem file

A

run bundle install

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

put rails app under version control

A

git init
git add -A
git commit -m “Initialize repository”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

pushing rails app after putting app under version control:

A

git remote add origin https://github.com/saweel/toy_app.git
git push -u origin master
(must create repository manually in github and then push it into it (thats what she said))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

typical first step when making a web application:

A

create data model, represenation of the structures needed by out application. in toy app, microblog, with only users and short posts. model for users and then model for microposts

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

data model for a user

A

interger identifier (id), name (string), email (string)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

model for micropost

A

id (integer) and content text (string), we want to associate each post with a user, so we need user_id (integer) for the post

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

user resource:

A

allow us to think users as objects that can be created, read, updated, and deleted through the web via the HTTP protocol. generage it using scaffold

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

use scaffold command to generate a resource (user) with parameters for attributed: name and email of type string. why dont you need to include id?

A

$ rails generate scaffold User name:string email:string. no need to include parameter for id; it is created automatically by rails for use as the primary key in the database

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

command to migrate database using Rake

A

$ bundle exec rake db:migrate. simply update the database with our new user data model. bundle exec to make sure rake corresponds to our gemfile version.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

rails s

A

shorcut for rails server

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

make utility

A

in unix, building executable programs from source codes. commonly used to compile code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

rake

A

rails uses rake extensively especially in developing database-backed web applications. rake db:migrate most common.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

(root URL)/users

A

page to list all users (index)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

/users/1

A

page to show user with id 1 (show)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

/users/new

A

page to make a new user (new)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

/users/1/edit

A

page to edit user with id 1 (edit)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

heroku create

A

create subdomain name for us.

20
Q

why is show and update action correspond to the same url

A

difference is the http request method they respond to.

21
Q

what is REST?

A

architectural style for developing distributed, networked systems and software applications such as web applications.

22
Q

how does REST model users and microposts?

A

application components are modeled as resources that can be created, read, updated, and deleted - operations both to the CRUD of relational database and to HTTP request methods:

23
Q

HTTP request methods:

A

POST, GET, PATCH DELETE (learn more later on)

24
Q

@users = User.all

A

list all users from User object in model database and place them in variable

25
Q

mvc model summary to go to /users: (long answer 8 steps)

A
  1. brower issues request for /users URL
  2. Rails routes /users to index action in Users controller
  3. index action asks User model to get all users
  4. User model pulls users from database
  5. User returns list of users to controller
  6. stores in @users variable, which is passed to the index view.
  7. the view uses embedded Ruby to render the page as HTML
  8. controller passes the HTML back to the browser
26
Q

mvc model of /users in more detail (long answer)

A
  1. request issued from browser (typing the url).
  2. sent to rails router which determines the controller action based on URL.
  3. action has @users = User.all
  4. which asks User model to get all users.
  5. stores users in @users.
  6. controller calls the html.erb view and iterates through @users and otputs HTML code
  7. view converts to HTML.
  8. returned by the controller to the browser for display.
27
Q

how URL to route knows which action to perform?

A

in config/routes.rb, we create controller action pair with URL. example: root ‘users#index’.

28
Q

weaknesses of User resource

A
  • no data validations(email can be wrong)
  • no authentication (cannot login or logout and no way to prevent user from perfoaming any operation)
  • no style or layout
  • no real understanding
  • no static pages (such as home or about)
  • no user passwords
  • no user images
  • no security
  • no following or followed, and more
29
Q

generate scaffold for Micropost

A

$ rails generate scaffold Micropost content: text user_id:integer

30
Q

after generating a new resource using scaffold, what do we have to do?

A

must run bundle exec rake db:migrate to update the database

31
Q

resources :microposts in route -

A

maps micropost URLs to actions in the Micropost controller

32
Q

constraint micropost text field to 140 characters(same as twitter)

A

app/models/micropost.rb

add:
validates :content, length: { maximum: 140 }

33
Q

write code to show a user has many microposts

A

has_many :microposts

app/models/user.rb

34
Q

code to show a micropost belongs to a user

A

app/models/micropost.rb

belongs_to :user

35
Q

$ rails console

A

useful tool for interacting with rails applications

36
Q

first_user = User.first (c)

A

store first user in variable

37
Q

first_user.microposts (c)

A

see all posts of user

38
Q

micropost = first_user.microposts.first

A

see first post of user

39
Q

micropost.user

A

see user info

40
Q

what do we gain from inheriting from ActiveRecord::Base

A

model objects gain the ability to communicate with the database, treat the database columns as Ruby attributes, and so on.

41
Q

user and micropost controller inheritance tree:

A

UserController

42
Q

inheriting from ActionController::Base provides what abilties?

A

manipulate model objects, filter inbound HTTP requests, and render views as HTML.

43
Q

to get heroku database to work:

A

$ heroku run rake db:migrate

44
Q

strength of this chapter:

A

high level overview of rails

  • intro to MVC
  • first taste of REST architecture
  • beginning data modeling
  • a live, database backed web application in production
45
Q

what is scaffolding

A

automatically creates code to model data and interact with it through the web