Skip to content

Instantly share code, notes, and snippets.

@progapandist
Last active November 10, 2020 10:27
Show Gist options
  • Save progapandist/e9013e645884ae8e52a00b804e1f79ec to your computer and use it in GitHub Desktop.
Save progapandist/e9013e645884ae8e52a00b804e1f79ec to your computer and use it in GitHub Desktop.
class CreateRestaurants < ActiveRecord::Migration[6.0]
def change
create_table :restaurants do |t|
t.string :name
t.string :address
t.integer :rating
t.timestamps
end
end
end
<%= form_for(@restaurant) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<br>
<%= f.label :address %>
<%= f.text_field :address, class: "my_class" %>
<br>
<%= f.label :rating %>
<%= f.number_field :rating %>
<br>
<%= f.submit %>
<% end %>
<h1>Simple Yelp</h1>
<ul>
<% @restaurants.each do |restaurant| %>
<li>
<%= link_to restaurant.name, restaurant_path(restaurant) %>
<%= link_to "🗑", restaurant_path(restaurant), method: :delete %>
</li>
<% end %>
</ul>
<%= form_for(@restaurant) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<br>
<%= f.label :address %>
<%= f.text_field :address, class: "my_class" %>
<br>
<%= f.label :rating %>
<%= f.number_field :rating %>
<br>
<%= f.submit %>
<% end %>
class Restaurant < ApplicationRecord
end
class RestaurantsController < ApplicationController
def index
# Get all restaurants from DB
@restaurants = Restaurant.all
end
def show
@restaurant = Restaurant.find(params[:id])
end
# Build an empty form
def new
@restaurant = Restaurant.new # empty object
end
# Where we actually create
def create
@restaurant = Restaurant.create(restaurant_params)
redirect_to restaurants_path
end
# Build an empty form for EDITING
def edit
@restaurant = Restaurant.find(params[:id])
end
# Where we actually update
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 parameters
def restaurant_params
# Forbidden object 👇
params.require(:restaurant).permit(:name, :address, :rating)
# Object with permitted fields 👉
end
end
Rails.application.routes.draw do
# # 1. GET /restaurants
# get "restaurants", to: "restaurants#index", as: :restaurants
# # GET /restaurants/new to give user a form
# get "restaurants/new", to: "restaurants#new", as: :new_restaurant
# # GET /restaurants/1 (2, 3, 456...)
# get "restaurants/:id", to: "restaurants#show", as: :restaurant
# # POST /restaurants
# post "restaurants", to: "restaurants#create"
# # GET /restaurants/:id/edit to give user an edit form
# get "restaurants/:id/edit", to: "restaurants#edit", as: :edit_restaurant
# # PATCH/PUT /restaurants/:id
# patch "restaurants/:id", to: "restaurants#update"
# # DELETE /restaurants/:id
# delete "restaurants/:id", to: "restaurants#destroy"
resources :restaurants
end
<h1> <%= @restaurant.name %> </h1>
<p>
Located at <%= @restaurant.address %>,
rated at <%= @restaurant.rating %> stars
</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment