From f50bf2fa5f06fa25ba0e72ca88060885f4a1a286 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20B=C3=B6hm?= Date: Tue, 20 Aug 2024 20:26:06 +0200 Subject: [PATCH] Added images to vehicles --- app/controllers/home_controller.rb | 2 ++ app/controllers/vehicles_controller.rb | 6 +++--- app/models/vehicle.rb | 9 ++++++++- app/views/vehicles/_form.html.erb | 7 +++++++ app/views/vehicles/_vehicle.html.erb | 5 +++++ 5 files changed, 25 insertions(+), 4 deletions(-) diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 95f2992..4f84207 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,4 +1,6 @@ class HomeController < ApplicationController + skip_before_action :authenticate + def index end end diff --git a/app/controllers/vehicles_controller.rb b/app/controllers/vehicles_controller.rb index e9376f1..e2b09f6 100644 --- a/app/controllers/vehicles_controller.rb +++ b/app/controllers/vehicles_controller.rb @@ -1,8 +1,8 @@ class VehiclesController < ApplicationController before_action :set_vehicle, only: %i[ show edit update destroy ] skip_before_action :authenticate - - #has_many_attached :images + + # has_many_attached :images # GET /vehicles or /vehicles.json def index @@ -68,6 +68,6 @@ class VehiclesController < ApplicationController # Only allow a list of trusted parameters through. def vehicle_params - params.require(:vehicle).permit(:vehicle_type, :car_brand, :model, :model_year, :fuel_type, :power_ps, :power_kw, :registered, :license_plate, :color, :note) + params.require(:vehicle).permit(:vehicle_type, :car_brand, :model, :model_year, :fuel_type, :power_ps, :power_kw, :registered, :license_plate, :color, :note, images: []) end end diff --git a/app/models/vehicle.rb b/app/models/vehicle.rb index 34ee24c..a5c64da 100644 --- a/app/models/vehicle.rb +++ b/app/models/vehicle.rb @@ -1,3 +1,10 @@ class Vehicle < ApplicationRecord - enum vehicle_type: { car: 0, truck: 1, motorcycle: 2, van: 3} + has_many_attached :images do |attachable| + attachable.variant :thumb, resize_to_limit: [ 100, 100 ] + attachable.variant :medium, resize_to_limit: [ 600, 600 ] + attachable.variant :big, resize_to_limit: [ 1000, 1000 ] + attachable.variant :hugh, resize_to_limit: [ 1920, 1920 ] + end + + enum vehicle_type: { car: 0, truck: 1, motorcycle: 2, van: 3 } end diff --git a/app/views/vehicles/_form.html.erb b/app/views/vehicles/_form.html.erb index f49debc..74e0da3 100644 --- a/app/views/vehicles/_form.html.erb +++ b/app/views/vehicles/_form.html.erb @@ -9,6 +9,13 @@ <% end %> +
+ <%= form.label :images %> + <% vehicle.images.each do |image| %> + <%= form.hidden_field :images, multiple: true, value: image.signed_id %> + <% end %> + <%= form.file_field :images, multiple: true, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %> +
<%= form.label :license_plate %> <%= form.text_field :license_plate, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %> diff --git a/app/views/vehicles/_vehicle.html.erb b/app/views/vehicles/_vehicle.html.erb index 03fa4e1..0c75f48 100644 --- a/app/views/vehicles/_vehicle.html.erb +++ b/app/views/vehicles/_vehicle.html.erb @@ -1,4 +1,9 @@
+

Bilder: + <% vehicle.images.each do |image| %> + <%= image_tag image.variant(:medium) %> + <% end %> +

Angemeldet: <%= vehicle.registered %>