Skip to content

Instantly share code, notes, and snippets.

@Alacrity01
Last active May 6, 2019 19:38
Show Gist options
  • Save Alacrity01/28c6219bf484942ba8379a2e72c75307 to your computer and use it in GitHub Desktop.
Save Alacrity01/28c6219bf484942ba8379a2e72c75307 to your computer and use it in GitHub Desktop.
Week 3, Day 1
http://localhost:3000/strawberry-pie Examples of possible URLs we have worked with
http://localhost:3000/api/recipe/strawberry-pie Command C will default to copying the line if nothing is highlighted.
http://localhost:3000/api/cookbook/3/recipes/4
http://localhost:3000/api/recipes/4 <-- Example of "restful routing"
http://localhost:3000/api/4
GET /photos photos#index display a list of all photos
GET /photos/new photos#new return an HTML form for creating a new photo
POST
GET
GET
PATCH/PUT
DELETE
Command P in sublime allows you to open a file by searching (VERY USEFUL)
Rails.application.routes.draw do
namespace :api do
get '/recipes' => 'recipes#index'
post '/recipes/' => 'recipes#create'
get '/recipes/:id' => 'recipes#show'
end
end
class Api::RecipesController < ApplicationController <- controller file
def index
@recipes = Recipe.all
render 'index.json.jbuilder' <- references index view file
end
def show
@recipe = Recipe.find(params[:id])
render 'show.json.jbuilder' <- references show view file
end
end
json.array! @recipes.each do |recipe| <- index view file (a view all)
json.id recipe.id
json.title recipe.title
json.chef recipe.chef
json.prep_time recipe.prep_time
json.ingredients recipe.ingredients Aim to do at least 10 of these, 5 without notes, for fluency
json.directions recipe.directions
json.image_url recipe.image_url
end
"Computers only know zero, one, and many."
URLs should be named plural
Uncomment cors.rb in config->initializers <- This file is not normally in Rails. It was added by Actualize to our Rails to make it easier.
===========================================================================================================================
Whiteboarding
Examples of how to whiteboard O O O X X X O = rock, X = red rock
1) list all red rocks
2) find all red rocks
• Stay simple. Stick to times loops. Don't use complex methods like .flatten or .sort.
• Think ahead so you aren't erasing constantly.
• Used commonly in interviews. If you don't ask questions, you will have points deducted.
• Give a narrative of your thought process or even stream of consciousness.
Problem: An array of 3 strings. Smash them together.
Whiteboarding:
input: array of strings. example: ["Peter", "Paul", "Mary"]
output: string "PeterPaulMary"
If the array is long, enough to show the pattern, beginning and ending
1) Take in a list of names
2) Create a location to collect our names
3) Add names one at a time to our collection
4) Give back our collection
def smasher(names)
collected_names = ""
index = 0
names.length.times do
collected_names += names[index]
index += 1
end
return collected_names
end
def smasher_each(names)
collected_names = ""
names.each do |name|
collected_names += name
end
return collected_names
end
__________________________________________________|
times | index | names[i] | coll_names |
________|_____________________|___________________|
1 | 0 | "Peter" | "Peter" | Sometimes visualization helps.
2 | 1 | "Paul" | "PeterPaul" | Drawing may
3 | 2 | "Mary" | "PeterPaulMary" |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment