Last active
November 10, 2020 10:27
-
-
Save progapandist/e9013e645884ae8e52a00b804e1f79ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= 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 %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= 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 %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Restaurant < ApplicationRecord | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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