Skip to content

Instantly share code, notes, and snippets.

@Alacrity01
Last active May 2, 2019 17:24
Show Gist options
  • Save Alacrity01/b9a75236c6dd61bc92bda690a184274a to your computer and use it in GitHub Desktop.
Save Alacrity01/b9a75236c6dd61bc92bda690a184274a to your computer and use it in GitHub Desktop.
Week 2, Day 4
string is the default for data type when creating attributes for a model
rails generate model Product name price:integer image_url description <-- all attributes of model Product will be strings except price (integer)
1. WEB REQUEST WINDOW ORDER
2. API ROUTE DESKTOP CHROME SUBLIME TERMINAL SLACK
3. API CONTROLLER (gets data from MODEL) <-- subject for today
4. JSON VIEW
5. WEB RESPONSE
rails new mini_capstone
cd mini_capstone
rake db:create
rails generate controller api/products <-- all controller names must be plural
subl .
rails generate model Product name price:integer image_url description
^ Name of model upper camelcase and singular.
Attributes default to string if data type not specified
rake db:migrate
rails console (or open seeds.rb and enter this data as one hash at a time followed by product.save -- remember to add {} around the hash--ex.: product = Product.new({all the hash data here})
product = Product.new(name: "Dialogue", price: 40, image_url: "google.com/images.html", description: "a conversation for sale")
product.save
git add --all
git commit -m "added products to seed.rb"
git push origin master
Now open config->routes / config->controller / views
Rails.application.routes.draw do <-- in the routes.rb file
namespace :api do
get '/all_products' => 'products#all_products'
get '/one_product' => 'products#one_product'
end
end
class Api::ProductsController < ApplicationController
def all_products
@products = Product.all
render 'all_products.json.jbuilder'
end
def one_product
@product = Product.first
render 'one_product.json.jbuilder'
end
end
json.array! @products.each do |product| <-- in a file all_products.json.jbuilder
json.id product.id
json.name product.name
json.price product.price
json.image_url product.image_url
json.description product.description
end
json.id @product.id <-- in file one_product.json.jbuilder
json.name @product.name
json.price @product.price
json.image_url @product.image_url
json.description @product.description
==============================================================================================
Falsey means evaluates as false.
Truthy means evaluates as true.
In Ruby, only nil or false are falsey.
!!(expression) is truthy if the expression is not false.
5 <-- evaluates as truthy
5 && 6 <-- evaluates as truthy and returns 6
"hello" && 5 && true <-- evaluates as truthy and returns true
5 && nil && "ghost" <-- evaluates as falsey and returns nil
false && "ghost" <-- evaluates as falsey and returns false
5 || 6 evaluates as truthy and returns 5
"hello" || 5 || false evaluates as truthy and returns "hello"
5 || 6 && "WAT" evaluates as truthy and returns 5
"hello" && 5 || true evaluates as truthy and returns 5
false || "kazoo" && 17 evaluates as truthy and returns 17
nil && 77 || "ghost" evaluates as falsey and returns nil
(55 && "hi") || 9 evaluates as truthy and returns "hi"
(5||6) && ("WAT"||9) evaluates as truthy and returns "WAT"
("hello" && 0)||(5 && true) evaluates as truthy and returns 0
(false || "kazoo" && true) || 17 evaluates as truthy and returns true
nil && (true || true || true) evaluates as falsey and returns nil
(true && false) || (true && false) evaluates as falsey and returns false
!(5 || 6) && ("WAT" || 9) returns false and evaluates as falsey
("hello" && 0) || !(5 && true) returns 0 and evaluates as truthy
(false || "kazoo" && [3,4] && !!17 returns true and evaluates as truthy
!nil && !(true || false || true) returns false and evaluates as falsey
START->false||nil||true/[4,3]&&!5||FINISH
==================================================================================
string query parameters (params) <-- http://www.google.com/search?q=dog <-- google search for dog uses string query parameter with ?q=dog added to the URL
URL segment params
body params (form params)
@message = params[:my_message] <-- is a hash that is given to you with rails in a web request that allows you access to the parameters
http://localhost:3000/api/query_params?my_message=yelp&message_2=welp&message_3=try again
^translates to
http://localhost:3000/api/query_params?my_message=yelp&message_2=welp&message_3=try%20again
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment