Skip to content

Instantly share code, notes, and snippets.

@jramiresbrito
Created December 1, 2020 21:10
Show Gist options
  • Save jramiresbrito/2aee3797bf6c4c9efa8156bb5b4d722e to your computer and use it in GitHub Desktop.
Save jramiresbrito/2aee3797bf6c4c9efa8156bb5b4d722e to your computer and use it in GitHub Desktop.
class CocktailsController < ApplicationController
def index
@cocktails = Cocktail.all
end
def show
@cocktail = Cocktail.find(params[:id])
@dose = Dose.new
@review = Review.new
end
def new
@cocktail = Cocktail.new
end
def create
@cocktail = Cocktail.new(cocktail_params)
if @cocktail.save
redirect_to cocktail_path(@cocktail)
else
render 'new'
end
end
private
def cocktail_params
params.require(:cocktail).permit(:name, :photo)
end
end
class DosesController < ApplicationController
# def new
# @cocktail = Cocktail.find(params[:cocktail_id])
# @dose = Dose.new
# end
def create
@cocktail = Cocktail.find(params[:cocktail_id])
@dose = Dose.new(dose_params)
@dose.cocktail = @cocktail
if @dose.save
redirect_to cocktail_path(@cocktail)
else
@review = Review.new
render "cocktails/show"
end
end
def destroy
@dose = Dose.find(params[:id])
@dose.destroy
redirect_to cocktail_path(@dose.cocktail)
end
private
def dose_params
params.require(:dose).permit(:description, :ingredient_id)
end
end
<div class="banner" style="background-image: linear-gradient(-225deg, rgba(0,101,168,0.6) 0%, rgba(0,36,61,0.6) 50%), url('<%= image_path 'cocktails.jpg' %>');">
<div class="banner-content">
<h1>Le Wagon's cocktails</h1>
<p>Awesome beverages for developers nightlife</p>
<%= link_to "Add a cocktail", new_cocktail_path, class: "btn btn-primary btn-lg" %>
</div>
</div>
<div class="container">
<div class="row">
<% @cocktails.each do |cocktail| %>
<div class="col-12 col-md-6 col-lg-3">
<% if cocktail.photo? %>
<% background_url = cl_image_path cocktail.photo, width: 400, height: 500, crop: :fill %>
<% else %>
<% background_url = 'https://source.unsplash.com/500x400/?cocktail' %>
<% end %>
<div class="card" style="background-image: linear-gradient(rgba(0,0,0,0.3), rgba(0,0,0,0.2)), url('<%= background_url %>');">
<div class="card-description">
<h2><%= cocktail.name %></h2>
</div>
<%= link_to "", cocktail_path(cocktail), class: 'card-link' %>
</div>
</div>
<% end %>
</div>
</div>
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<h1>Create a new cocktail</h1>
<%= simple_form_for @cocktail do |f| %>
<%= f.input :name %>
<%= f.input :photo %>
<%= f.input :photo_cache, as: :hidden %>
<%= f.button :submit, class: 'btn btn-primary' %>
<% end %>
</div>
</div>
</div>
<h3>Add a new dose</h3>
<%= simple_form_for [cocktail, dose] do |f| %>
<%= f.association :ingredient, input_html: { class: "beautiful-dropdown" }, include_hidden: false %>
<%= f.input :description %>
<%= f.button :submit, class: "btn btn-primary" %>
<% end %>
<h3>Add a review</h3>
<%= simple_form_for [cocktail, review] do |f| %>
<%= f.input :rating, collection: 0..5 %>
<%= f.input :content %>
<%= f.submit class: "btn btn-primary"%>
<% end %>
class ReviewsController < ApplicationController
def create
@cocktail = Cocktail.find(params[:cocktail_id])
@review = Review.new(review_params)
@review.cocktail = @cocktail
if @review.save
redirect_to cocktail_path(@cocktail)
else
@dose = Dose.new
render "cocktails/show"
end
end
private
def review_params
params.require(:review).permit(:rating, :content)
end
end
<div class="container">
<h1><%= @cocktail.name %></h1>
<%= link_to "Back to all cocktails", root_path %>
<br>
<%= cl_image_tag @cocktail.photo, height: 300, width: 400, crop: :fill %>
<div class="row">
<div class="col-sm-12 col-md-6">
<h3>Recipe</h3>
<table class="table table-bordered">
<tbody>
<thead>
<th>Ingredient</th>
<th>Description</th>
<th>Action</th>
</thead>
<% @cocktail.doses.each do |dose| %>
<tr>
<td><%= dose.ingredient.name %></td>
<td><%= dose.description %></td>
<td>
<%= link_to dose_path(dose), method: :delete, data: { confirm: "Are you sure?" } do %>
<i class="fa fa-trash destroy_dose"></i>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
<div class="col-sm-12 col-md-6">
<%= render "doses/new", cocktail: @cocktail, dose: @dose %>
</div>
</div>
<h3>Reviews</h3>
<% if @cocktail.reviews.empty? %>
<p>Be the first to let a review</p>
<% else %>
<ul class="list-group">
<% @cocktail.reviews.each do |review| %>
<li class="list-group-item d-flex justify-content-between align-items-center">
<span class="badge badge-primary badge-pill">
<% counter = 0 %>
<% 5.times do %>
<i class="fa <%= review.rating > counter ? 'fa-star' : 'fa-star-o' %>"></i>
<% counter += 1 %>
<% end %>
</span>
<%= review.content %>
</li>
<% end %>
</ul>
<% end %>
<%= render "reviews/new", cocktail: @cocktail, review: @review %>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment