Skip to content

Instantly share code, notes, and snippets.

@rubys
Created June 11, 2024 13:43
Show Gist options
  • Save rubys/c9f0e28727b58365477ce2b858f26355 to your computer and use it in GitHub Desktop.
Save rubys/c9f0e28727b58365477ce2b858f26355 to your computer and use it in GitHub Desktop.
Active Storage Demo - full files
<div id="<%= dom_id item %>">
<p class="my-5">
<strong class="block font-medium mb-1">Name:</strong>
<%= item.name %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Contents:</strong>
<% if item.contents.attached? %>
<% if item.contents.content_type.start_with? 'image/' %>
<img src=<%= item.contents.url %> alt="<%= item.contents.filename %>">
<% elsif item.contents.content_type.start_with? 'audio/' %>
<audio controls>
<source src=<%= item.contents.url %> type=<%= item.contents.content_type %>>
</audio>
<% elsif item.contents.content_type.start_with? 'video/' %>
<video controls>
<source src=<%= item.contents.url %> type=<%= item.contents.content_type %>>
</audio>
<% else %>
<%= link_to item.contents.filename, item.contents %>
<% end %>
<% end %>
</p>
</div>
class ItemsController < ApplicationController
before_action :set_item, only: %i[ show edit update destroy ]
include ActiveStorage::SetCurrent
# GET /items or /items.json
def index
@items = Item.all
end
# GET /items/1 or /items/1.json
def show
end
# GET /items/new
def new
@item = Item.new
end
# GET /items/1/edit
def edit
end
# POST /items or /items.json
def create
@item = Item.new(item_params)
respond_to do |format|
if @item.save
format.html { redirect_to item_url(@item), notice: "Item was successfully created." }
format.json { render :show, status: :created, location: @item }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @item.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /items/1 or /items/1.json
def update
respond_to do |format|
if @item.update(item_params)
format.html { redirect_to item_url(@item), notice: "Item was successfully updated." }
format.json { render :show, status: :ok, location: @item }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @item.errors, status: :unprocessable_entity }
end
end
end
# DELETE /items/1 or /items/1.json
def destroy
@item.destroy!
respond_to do |format|
format.html { redirect_to items_url, notice: "Item was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_item
@item = Item.find(params[:id])
end
# Only allow a list of trusted parameters through.
def item_params
params.require(:item).permit(:name, :contents)
end
end
Rails.application.routes.draw do
resources :items
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
# Can be used by load balancers and uptime monitors to verify that the app is live.
get "up" => "rails/health#show", as: :rails_health_check
# Defines the root path route ("/")
root "items#index"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment