Add: image database table, image test site, image upload support
This commit is contained in:
parent
73d63f92e4
commit
982b72494c
13 changed files with 207 additions and 1 deletions
25
app/controllers/test/images_controller.rb
Normal file
25
app/controllers/test/images_controller.rb
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
class Test::ImagesController < ApplicationController
|
||||
def index
|
||||
@images = Image.all
|
||||
@image = Image.new
|
||||
end
|
||||
|
||||
def create
|
||||
@image = Image.new(image_params)
|
||||
|
||||
if @image.save
|
||||
respond_to do |format|
|
||||
format.turbo_stream
|
||||
format.html { redirect_to test_images_path }
|
||||
end
|
||||
else
|
||||
render :index, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def image_params
|
||||
params.require(:image).permit(:title, :file)
|
||||
end
|
||||
end
|
||||
2
app/helpers/test/images_helper.rb
Normal file
2
app/helpers/test/images_helper.rb
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
module Test::ImagesHelper
|
||||
end
|
||||
5
app/models/image.rb
Normal file
5
app/models/image.rb
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
class Image < ApplicationRecord
|
||||
has_one_attached :file
|
||||
|
||||
validates :title, :file, presence: true
|
||||
end
|
||||
6
app/views/test/images/_image.html.erb
Normal file
6
app/views/test/images/_image.html.erb
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
<div class="image-entry">
|
||||
<h3><%= image.title %></h3>
|
||||
<% if image.file.attached? %>
|
||||
<%= image_tag image.file, width: 300 %>
|
||||
<% end %>
|
||||
</div>
|
||||
1
app/views/test/images/create.turbo_stream.erb
Normal file
1
app/views/test/images/create.turbo_stream.erb
Normal file
|
|
@ -0,0 +1 @@
|
|||
<%= turbo_stream.prepend "images", partial: "test/images/image", locals: { image: @image } %>
|
||||
20
app/views/test/images/index.html.erb
Normal file
20
app/views/test/images/index.html.erb
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
<h1>Image Upload (Test)</h1>
|
||||
|
||||
<%= form_with model: @image, url: test_images_path, data: { turbo_stream: true } do |form| %>
|
||||
<%= form.label :title %>
|
||||
<%= form.text_field :title %><br>
|
||||
|
||||
<%= form.label :file %>
|
||||
<%= form.file_field :file %><br>
|
||||
|
||||
<%= form.submit "Upload" %>
|
||||
<% end %>
|
||||
|
||||
<hr>
|
||||
|
||||
<h2>Uploaded Images</h2>
|
||||
<div id="images">
|
||||
<%= turbo_frame_tag "images" do %>
|
||||
<%= render @images %>
|
||||
<% end %>
|
||||
</div>
|
||||
|
|
@ -1,4 +1,11 @@
|
|||
Rails.application.routes.draw do
|
||||
namespace :test do
|
||||
resources :images
|
||||
get "images/index"
|
||||
get "images/new"
|
||||
get "images/create"
|
||||
get "images/show"
|
||||
end
|
||||
resources :notes
|
||||
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,57 @@
|
|||
# This migration comes from active_storage (originally 20170806125915)
|
||||
class CreateActiveStorageTables < ActiveRecord::Migration[7.0]
|
||||
def change
|
||||
# Use Active Record's configured type for primary and foreign keys
|
||||
primary_key_type, foreign_key_type = primary_and_foreign_key_types
|
||||
|
||||
create_table :active_storage_blobs, id: primary_key_type do |t|
|
||||
t.string :key, null: false
|
||||
t.string :filename, null: false
|
||||
t.string :content_type
|
||||
t.text :metadata
|
||||
t.string :service_name, null: false
|
||||
t.bigint :byte_size, null: false
|
||||
t.string :checksum
|
||||
|
||||
if connection.supports_datetime_with_precision?
|
||||
t.datetime :created_at, precision: 6, null: false
|
||||
else
|
||||
t.datetime :created_at, null: false
|
||||
end
|
||||
|
||||
t.index [ :key ], unique: true
|
||||
end
|
||||
|
||||
create_table :active_storage_attachments, id: primary_key_type do |t|
|
||||
t.string :name, null: false
|
||||
t.references :record, null: false, polymorphic: true, index: false, type: foreign_key_type
|
||||
t.references :blob, null: false, type: foreign_key_type
|
||||
|
||||
if connection.supports_datetime_with_precision?
|
||||
t.datetime :created_at, precision: 6, null: false
|
||||
else
|
||||
t.datetime :created_at, null: false
|
||||
end
|
||||
|
||||
t.index [ :record_type, :record_id, :name, :blob_id ], name: :index_active_storage_attachments_uniqueness, unique: true
|
||||
t.foreign_key :active_storage_blobs, column: :blob_id
|
||||
end
|
||||
|
||||
create_table :active_storage_variant_records, id: primary_key_type do |t|
|
||||
t.belongs_to :blob, null: false, index: false, type: foreign_key_type
|
||||
t.string :variation_digest, null: false
|
||||
|
||||
t.index [ :blob_id, :variation_digest ], name: :index_active_storage_variant_records_uniqueness, unique: true
|
||||
t.foreign_key :active_storage_blobs, column: :blob_id
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def primary_and_foreign_key_types
|
||||
config = Rails.configuration.generators
|
||||
setting = config.options[config.orm][:primary_key_type]
|
||||
primary_key_type = setting || :primary_key
|
||||
foreign_key_type = setting || :bigint
|
||||
[ primary_key_type, foreign_key_type ]
|
||||
end
|
||||
end
|
||||
9
db/migrate/20250507082751_create_images.rb
Normal file
9
db/migrate/20250507082751_create_images.rb
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
class CreateImages < ActiveRecord::Migration[8.0]
|
||||
def change
|
||||
create_table :images do |t|
|
||||
t.string :title
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
||||
39
db/schema.rb
generated
39
db/schema.rb
generated
|
|
@ -10,11 +10,48 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema[8.0].define(version: 2025_05_06_085403) do
|
||||
ActiveRecord::Schema[8.0].define(version: 2025_05_07_082751) do
|
||||
create_table "active_storage_attachments", force: :cascade do |t|
|
||||
t.string "name", null: false
|
||||
t.string "record_type", null: false
|
||||
t.bigint "record_id", null: false
|
||||
t.bigint "blob_id", null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
|
||||
t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
|
||||
end
|
||||
|
||||
create_table "active_storage_blobs", force: :cascade do |t|
|
||||
t.string "key", null: false
|
||||
t.string "filename", null: false
|
||||
t.string "content_type"
|
||||
t.text "metadata"
|
||||
t.string "service_name", null: false
|
||||
t.bigint "byte_size", null: false
|
||||
t.string "checksum"
|
||||
t.datetime "created_at", null: false
|
||||
t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
|
||||
end
|
||||
|
||||
create_table "active_storage_variant_records", force: :cascade do |t|
|
||||
t.bigint "blob_id", null: false
|
||||
t.string "variation_digest", null: false
|
||||
t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
|
||||
end
|
||||
|
||||
create_table "images", force: :cascade do |t|
|
||||
t.string "title"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
end
|
||||
|
||||
create_table "notes", force: :cascade do |t|
|
||||
t.string "title"
|
||||
t.text "body"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
end
|
||||
|
||||
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
|
||||
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
|
||||
end
|
||||
|
|
|
|||
23
test/controllers/test/images_controller_test.rb
Normal file
23
test/controllers/test/images_controller_test.rb
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
require "test_helper"
|
||||
|
||||
class Test::ImagesControllerTest < ActionDispatch::IntegrationTest
|
||||
test "should get index" do
|
||||
get test_images_index_url
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should get new" do
|
||||
get test_images_new_url
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should get create" do
|
||||
get test_images_create_url
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should get show" do
|
||||
get test_images_show_url
|
||||
assert_response :success
|
||||
end
|
||||
end
|
||||
7
test/fixtures/images.yml
vendored
Normal file
7
test/fixtures/images.yml
vendored
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||
|
||||
one:
|
||||
title: MyString
|
||||
|
||||
two:
|
||||
title: MyString
|
||||
7
test/models/image_test.rb
Normal file
7
test/models/image_test.rb
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
require "test_helper"
|
||||
|
||||
class ImageTest < ActiveSupport::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue