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>
|
||||
Loading…
Add table
Add a link
Reference in a new issue