Skip to content

Instantly share code, notes, and snippets.

@progapandist
Created February 16, 2021 10:52
Show Gist options
  • Save progapandist/32ad692f0a003f223fa83aa19d079fed to your computer and use it in GitHub Desktop.
Save progapandist/32ad692f0a003f223fa83aa19d079fed to your computer and use it in GitHub Desktop.
class CreateRestaurants < ActiveRecord::Migration[6.1]
def change
create_table :restaurants do |t|
t.string :name
t.integer :rating, null: false, default: 0
t.string :address
t.timestamps
end
end
end
class AddPhoneToRestaurants < ActiveRecord::Migration[6.1]
def change
add_column :restaurants, :phone, :string
end
end
<h1>Edit <%= @restaurant.name %></h1>
<%# Existing object, so build a PUT form to update one %>
<%= form_for @restaurant do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<br>
<%= f.label :rating %>
<%= f.number_field :rating %>
<br>
<%= f.submit %>
<% end %>
<h1>All restaurants</h1>
<ul>
<% @restaurants.each do |restaurant| %>
<li>
<%= link_to restaurant.name, restaurant_path(restaurant) %>
| <%= link_to "🗑", restaurant_path(restaurant), method: :delete %>
</li>
<% end %>
</ul>
<h1>Submit a restaurant to our app</h1>
<%# Empty object, so build a POST form to create one %>
<%= form_for @restaurant do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<br>
<%= f.label :rating %>
<%= f.number_field :rating %>
<br>
<%= f.submit %>
<% end %>
class Restaurant < ApplicationRecord
end
class RestaurantsController < ApplicationController
# I Should Never Call Ex Upon Drinking
def index
@restaurants = Restaurant.all
end
def show
@restaurant = Restaurant.find(params[:id])
end
def new
# Rails will figure out where to point the form
# based on the class of that object and MAGIC CONVENTIONS
@restaurant = Restaurant.new
end
def create
@restaurant = Restaurant.new(restaurant_params)
@restaurant.save
redirect_to restaurant_path(@restaurant)
end
def edit
@restaurant = Restaurant.find(params[:id])
end
def update
@restaurant = Restaurant.find(params[:id])
@restaurant.update(restaurant_params)
redirect_to restaurant_path(@restaurant)
end
def destroy
@restaurant = Restaurant.find(params[:id])
@restaurant.destroy
redirect_to restaurants_path
end
private
# Strong params
def restaurant_params
params.require(:restaurant).permit(:name, :rating)
end
end
Rails.application.routes.draw do
# get "restaurants", to: "restaurants#index"
# get "restaurants/new", to: "restaurants#new", as: :new_restaurant
# get "restaurants/:id", to: "restaurants#show", as: :restaurant
# post "restaurants", to: "restaurants#create"
# get "restaurants/:id/edit", to: "restaurants#edit", as: :edit_restaurant
# put "restaurants/:id", to: "restaurants#update"
# patch "restaurants/:id", to: "restaurants#update"
# delete "restaurants/:id", to: "restaurants#destroy"
resources :restaurants
end
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2021_02_16_082443) do
create_table "restaurants", force: :cascade do |t|
t.string "name"
t.integer "rating", default: 0, null: false
t.string "address"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "phone"
end
end
<h1><%= @restaurant.name %></h1>
<div>
Rated at <%= @restaurant.rating %> stars.
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment