Para la gestión de imágenes no utilizo ninguna gema, lo hago con la opción que da rails 6, con active_storage.
Por ahora no hay nada especial, el controlador está como se genera, no le he realizado ningún cambio en la parte de actualizar/editar y en el modelo solo he añadido el has_many_attached :images
para que guarde multiples imagenes.
La pregunta, entonces, es, como sería la forma de poder añadir las nuevas imágenes sin borrar las anteriores??
# PATCH/PUT /albums/1 or /albums/1.json
def update
respond_to do |format|
if @album.update(album_params)
format.html { redirect_to albums_path, notice: "Album was successfully updated." }
format.json { render :show, status: :ok, location: @album }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @album.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_album
@album = Album.find(params[:id])
end
# Only allow a list of trusted parameters through.
def album_params
params.require(:album).permit(:title, :date_event, :password, images: [])
end
el schema.db:
ActiveRecord::Schema.define(version: 2021_03_16_193227) do
create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", 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", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", 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", null: false
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", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", 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 "albums", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "title"
t.date "date_event"
t.string "password"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "users", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "role", default: ""
t.string "name"
t.string "passwd_album"
t.string "last_name"
t.string "address"
t.string "province"
t.integer "cod_postal"
t.integer "phone"
t.date "birthdate"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
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