all Flashcards
what is a web framework
give baseline tools such as routing, asset management, etc so you don’t have to start from stratch
what is the MVC framework
Model - View - Controller
what is an opinionated framework
convention over configuation . have a certain way to set up organization
what goes in the app file
models, view, controllers
what goes in config file
routes
what goes in db file
migration files, schema, seed
syntax for route in rails
get “/route”, to “route#index”
syntax for controller
class FileController < ApplicationController
def index
end
end
what is the Model
like a chef. communicates with database via Active Record
what is the Controller
like the waiter. transmits data from user to the models and back to the user to view
what is the view
like the table. where date is visible to user
why use rails generators
less time consuming
help prevent errors and debug
types of generator
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
syntax for rails generator
rails g model TableName column1 column2:integer
what does rail generator default to for type
string
what do you run to save a migration
rails db:migrate
syntax for seed
Table.create(column: “data”, column: “data”)
what is the basic HTTP work flow
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
Which name is singular and which is plural in model and controller names
model = singular
controller = plural
what is rails is like binding.pry
byebug
Process for Dynamic Routes
routes that include parameter
routes send to controller
get instance associated with params
static routes
always have fixed routes (/user)
dynamic routes
render different data based on the parameters (/user/:id)
what does the code in controller look like for dynamic routes
def show
user = user.find(param [:id])
render json: user
end
what do static routes look like in controller
def index
users = user.all
render json: users
end
what are the 2 ways to custom render data in the controller
selecting specific with only: [:id, :name, :address]
using exclude to state which ones not to add except: [:created_at, :updated_at]
how to include methods in the controller that will be sent to user
add methods to the render json
render json: user, except: [:created_at], methods: [summary]
syntax for writing error messages in controller
else
render json: {error: ‘User not found’}, status: :not_found
what is REST
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
7 routes that are apart of REST
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
how do you see all the routes listed in terminal
rails routes
what does resource do
in the routes file is creates all 7 routes
to select which routes using resource
resource: class , only [:index, :create, :delete]
what do strong params do
method that allows only certain parameters
how do create strong params
def create
user = User.create(user_params)
render json: user, status: :created
end
private
def user_params
params.permit(:name, :email)
end
end
why use strong params
stops mass assignment
cleaner code
how to add column using migration
rails g migration AddColumnToClass column:integer
controller syntax for update existing element
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
what are validations in rails
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
why do we have validations
protects database from bad data
when are validations checked
when adding or updating data through ruby/rails
what is syntax for setting up validations
2 arguments the attribute and how to validate
validates :name, presence: true
how do you trigger validations without touching database
.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
what are built in validators
length - validates :name, length:{minimum: 2}
uniqueness - validates :email, uniqueness: true
controller validations manually with if else using valid? syntax
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
how do you format error messages
user.errors
user.errors.full_messages
steps for creating rails API
- generate rails application
- create model
- migrations
- controller
- routes
- seed database
- set up routes
where do you debug 404 error
in browser under network tab and then preview tab
where do you look at syntax error
return JSON response in controller and frontend fetch
where do you look server error
in server log: it gives error itself and file/name of error
what do active record associations do
allow us to connect complex networks of models
how are models connected
with foreign keys
how do you setup many to many
with a join table that has foreign key from both models
the join table for many to many would have
foriegn keys from both tables and belongs_to: in the model
in the many to many relationship what goes into the models that both have many
has_many :join_table
has_many :other_table, through: :join_table
how do you show associated data
use include: :join_table
how do you delete assocaited data
dependent: :destroy
what are cookies
small pieces of information that are sent from the server to the client and stored in client
what do cookies help with
making HTTP stateful
what do cookies store
sessions information such as login, shopping cart, location, personalization, tracking information
what do you use in the controllers to save cookies
sessions controller
what is authentication
application confirm users are who they say they are
what is the flow for authentication
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
what is the session controller create syntax
def create
user = User.find_by(username: params[:username])
session[:user_id] = user.id
render json: user
end
end