Last active
November 11, 2020 10:27
-
-
Save progapandist/fdde589046c7092d7fe4199a5d136148 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 :stars | |
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
class CreateReviews < ActiveRecord::Migration[6.0] | |
def change | |
create_table :reviews do |t| | |
t.text :content | |
t.references :restaurant, null: false, foreign_key: true | |
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
<h1>Editing Restaurant</h1> | |
<%= render 'form', restaurant: @restaurant %> | |
<%= link_to 'Show', @restaurant %> | | |
<%= link_to 'Back', restaurants_path %> |
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>Add review to a <%= @restaurant.name %> </h1> | |
<!-- POST /restaurants/:restaurant_id/reviews --> | |
<%= simple_form_for([@restaurant, @review]) do |f| %> | |
<%= f.input :content %> | |
<%= f.submit class: "btn btn-secondary" %> | |
<% 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 | |
has_many :reviews | |
# Allows you to do: | |
# resto = Restaurant.find(1) | |
# resto.reviews => Returns array of Review | |
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 | |
before_action :set_restaurant, only: [:show, :edit, :update, :destroy] | |
# GET /restaurants | |
def index | |
@restaurants = Restaurant.all | |
end | |
# GET /restaurants/1 | |
def show | |
end | |
# GET /restaurants/new | |
def new | |
@restaurant = Restaurant.new | |
end | |
# GET /restaurants/1/edit | |
def edit | |
end | |
# POST /restaurants | |
def create | |
@restaurant = Restaurant.new(restaurant_params) | |
if @restaurant.save | |
redirect_to @restaurant, notice: 'Restaurant was successfully created.' | |
else | |
render :new | |
end | |
end | |
# PATCH/PUT /restaurants/1 | |
def update | |
if @restaurant.update(restaurant_params) | |
redirect_to @restaurant, notice: 'Restaurant was successfully updated.' | |
else | |
render :edit | |
end | |
end | |
# DELETE /restaurants/1 | |
def destroy | |
@restaurant.destroy | |
redirect_to restaurants_url, notice: 'Restaurant was successfully destroyed.' | |
end | |
private | |
# Use callbacks to share common setup or constraints between actions. | |
def set_restaurant | |
@restaurant = Restaurant.find(params[:id]) | |
end | |
# Only allow a trusted parameter "white list" through. | |
def restaurant_params | |
params.require(:restaurant).permit(:name, :address, :stars) | |
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
class Review < ApplicationRecord | |
belongs_to :restaurant | |
# Allows you to: | |
# rev = Review.find(1) | |
# rev.restaurant => Returns parent Restaurant | |
validates :content, presence: { message: "NO YOU CANT" } | |
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 ReviewsController < ApplicationController | |
def new | |
@restaurant = Restaurant.find(params[:restaurant_id]) | |
@review = Review.new | |
end | |
# POST /restaurants/3/reviews | |
# Body contains data for attributes | |
def create | |
@restaurant = Restaurant.find(params[:restaurant_id]) | |
@review = Review.new(review_params) | |
@review.restaurant = @restaurant | |
if @review.save # No guarantee it will save! | |
# Happy path :) | |
# Don't redirect if save went wrong | |
redirect_to restaurant_path(@restaurant) | |
else | |
# Sad path :( | |
render "new" | |
end | |
end | |
def destroy | |
@review = Review.find(params[:id]) | |
@review.destroy | |
redirect_to restaurant_path(@review.restaurant) | |
end | |
private | |
def review_params | |
params.require(:review).permit(:content) | |
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 | |
resources :restaurants do | |
# Nested | |
resources :reviews, only: [:new, :create] | |
end | |
# Not nested | |
resources :reviews, only: :destroy | |
# get "restaurants/:restaurant_id/reviews/new", to: "reviews#new" | |
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
<p id="notice"><%= notice %></p> | |
<p> | |
<strong>Name:</strong> | |
<%= @restaurant.name %> | |
</p> | |
<p> | |
<strong>Address:</strong> | |
<%= @restaurant.address %> | |
</p> | |
<p> | |
<strong>Stars:</strong> | |
<%= @restaurant.stars %> | |
</p> | |
<div> | |
<ul> | |
<% @restaurant.reviews.each do |review| %> | |
<li> | |
<%= review.content %> | <%= link_to "❌", review_path(review), method: :delete %> | |
</li> | |
<% end %> | |
</ul> | |
</div> | |
<%= link_to 'Edit', edit_restaurant_path(@restaurant) %> | | |
<%= link_to 'Back', restaurants_path %> | | |
<%= link_to 'Leave a review', new_restaurant_review_path(@restaurant) %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment