all Flashcards

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

what is a web framework

A

give baseline tools such as routing, asset management, etc so you don’t have to start from stratch

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

what is the MVC framework

A

Model - View - Controller

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

what is an opinionated framework

A

convention over configuation . have a certain way to set up organization

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

what goes in the app file

A

models, view, controllers

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

what goes in config file

A

routes

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

what goes in db file

A

migration files, schema, seed

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

syntax for route in rails

A

get “/route”, to “route#index”

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

syntax for controller

A

class FileController < ApplicationController
def index
end
end

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

what is the Model

A

like a chef. communicates with database via Active Record

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

what is the Controller

A

like the waiter. transmits data from user to the models and back to the user to view

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

what is the view

A

like the table. where date is visible to user

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

why use rails generators

A

less time consuming
help prevent errors and debug

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

types of generator

A

rails g model - create model
rails g controller - create controller
rails g migration - create new table
rails g resources - create model, controller, migration, view folder and routes

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

syntax for rails generator

A

rails g model TableName column1 column2:integer

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

what does rail generator default to for type

A

string

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

what do you run to save a migration

A

rails db:migrate

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

syntax for seed

A

Table.create(column: “data”, column: “data”)

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

what is the basic HTTP work flow

A

client request to server (fetch)
request goes to server where router interpret request and sends message to controller to map route
controller uses model to access database
controller uses data to render view (HTML or JSON)
server returns response

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

Which name is singular and which is plural in model and controller names

A

model = singular
controller = plural

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

what is rails is like binding.pry

A

byebug

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

Process for Dynamic Routes

A

routes that include parameter
routes send to controller
get instance associated with params

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

static routes

A

always have fixed routes (/user)

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

dynamic routes

A

render different data based on the parameters (/user/:id)

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

what does the code in controller look like for dynamic routes

A

def show
user = user.find(param [:id])
render json: user
end

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

what do static routes look like in controller

A

def index
users = user.all
render json: users
end

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

what are the 2 ways to custom render data in the controller

A

selecting specific with only: [:id, :name, :address]
using exclude to state which ones not to add except: [:created_at, :updated_at]

27
Q

how to include methods in the controller that will be sent to user

A

add methods to the render json
render json: user, except: [:created_at], methods: [summary]

28
Q

syntax for writing error messages in controller

A

else
render json: {error: ‘User not found’}, status: :not_found

29
Q

what is REST

A

standardized naming pattern used across many program languages to help developers determine how routing should be created

it is the standard way of mapping CRUD actions from the HTTP methods

30
Q

7 routes that are apart of REST

A

index | get | display all objects
new | get | form to create new object
create | post | creates new object on server
show | get | display specific object
edit | get | form to edit specific object
update | patch | update specific object on server
destroy | delete | deletes specific object

31
Q

how do you see all the routes listed in terminal

A

rails routes

32
Q

what does resource do

A

in the routes file is creates all 7 routes

33
Q

to select which routes using resource

A

resource: class , only [:index, :create, :delete]

34
Q

what do strong params do

A

method that allows only certain parameters

35
Q

how do create strong params

A

def create
user = User.create(user_params)
render json: user, status: :created
end

private

def user_params
params.permit(:name, :email)
end
end

36
Q

why use strong params

A

stops mass assignment
cleaner code

37
Q

how to add column using migration

A

rails g migration AddColumnToClass column:integer

38
Q

controller syntax for update existing element

A

def update
user = User.find_by(id: params [:id])
if user
user.update(user_params)
render json: user
else
render json: {error: “User not found”}, status: :not_found
end

private
def user_params
params.permit(:name, :email, :password)
end
end

39
Q

what are validations in rails

A

special methods calls that go at thetop of model class definitions and prevent them from being saved to database if the data isn’t correct

40
Q

why do we have validations

A

protects database from bad data

41
Q

when are validations checked

A

when adding or updating data through ruby/rails

42
Q

what is syntax for setting up validations

A

2 arguments the attribute and how to validate
validates :name, presence: true

43
Q

how do you trigger validations without touching database

A

.valid? method

def create
user = User.create(user_params)
if user.valid?
render json: user, status: :created
else
render json: {errors: person.errors.full_messages}, status: :unprocessable_ entity
end
end

44
Q

what are built in validators

A

length - validates :name, length:{minimum: 2}
uniqueness - validates :email, uniqueness: true

45
Q

controller validations manually with if else using valid? syntax

A

def create
user = User.create(user_params)
if user.valid?
render json: user, status: :created
else
render json: {errors: bird.errors},
status: :unprocessable entity
end
end

46
Q

how do you format error messages

A

user.errors
user.errors.full_messages

47
Q

steps for creating rails API

A
  1. generate rails application
  2. create model
  3. migrations
  4. controller
  5. routes
  6. seed database
  7. set up routes
48
Q

where do you debug 404 error

A

in browser under network tab and then preview tab

49
Q

where do you look at syntax error

A

return JSON response in controller and frontend fetch

50
Q

where do you look server error

A

in server log: it gives error itself and file/name of error

51
Q

what do active record associations do

A

allow us to connect complex networks of models

52
Q

how are models connected

A

with foreign keys

53
Q

how do you setup many to many

A

with a join table that has foreign key from both models

54
Q

the join table for many to many would have

A

foriegn keys from both tables and belongs_to: in the model

55
Q

in the many to many relationship what goes into the models that both have many

A

has_many :join_table
has_many :other_table, through: :join_table

56
Q

how do you show associated data

A

use include: :join_table

57
Q

how do you delete assocaited data

A

dependent: :destroy

58
Q

what are cookies

A

small pieces of information that are sent from the server to the client and stored in client

59
Q

what do cookies help with

A

making HTTP stateful

60
Q

what do cookies store

A

sessions information such as login, shopping cart, location, personalization, tracking information

61
Q

what do you use in the controllers to save cookies

A

sessions controller

62
Q

what is authentication

A

application confirm users are who they say they are

63
Q

what is the flow for authentication

A

user uses login form
on submit a post request is sent
routes to post “/login”, to “sessions#create”
session controller set cookie as users id into session hash

64
Q

what is the session controller create syntax

A

def create
user = User.find_by(username: params[:username])
session[:user_id] = user.id
render json: user
end
end