-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add routes to upsert and delete images
- Loading branch information
1 parent
663c720
commit dbe7bca
Showing
25 changed files
with
395 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
class ImagesController < ApplicationController | ||
before_action :set_image, only: %i[show edit update destroy] | ||
|
||
def index | ||
@images = Image.all | ||
end | ||
|
||
def show; end | ||
|
||
def new | ||
@image = Image.new | ||
end | ||
|
||
def edit; end | ||
|
||
def create | ||
@image = Image.new(image_params) | ||
|
||
if @image.save | ||
redirect_to @image, notice: 'Imagem criada!' | ||
else | ||
render :new, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def update | ||
if @image.update(image_params) | ||
redirect_to @image, notice: 'Imagem atualizada!' | ||
else | ||
render :edit, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def destroy | ||
@image.destroy | ||
|
||
redirect_to images_url, notice: 'Imagem deletada!' | ||
end | ||
|
||
private | ||
|
||
def set_image | ||
@image = Image.find(params[:id]) | ||
end | ||
|
||
def image_params | ||
params.require(:image).permit(:title, :file, :user_id) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module ImagesHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Image < ApplicationRecord | ||
belongs_to :user | ||
|
||
has_one_attached :file | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<%= form_with(model: image, local: true) do |form| %> | ||
<% if image.errors.any? %> | ||
<div id="error_explanation"> | ||
<h2><%= pluralize(image.errors.count, "error") %> prohibited this image from being saved:</h2> | ||
|
||
<ul> | ||
<% image.errors.full_messages.each do |message| %> | ||
<li><%= message %></li> | ||
<% end %> | ||
</ul> | ||
</div> | ||
<% end %> | ||
<%= form.hidden_field(:user_id, value: current_user.id) %> | ||
|
||
<div class="form-group"> | ||
<%= form.label :title, 'Título', class: 'form-label' %> | ||
<%= form.text_field :title, required: true, class: 'form-control' %> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<div class="custom-file"> | ||
<%= form.file_field :file, required: true, class: 'custom-file-input' %> | ||
<%= form.label :file, 'Escolha o arquivo...', class: 'custom-file-label' %> | ||
<div class="invalid-feedback">Example invalid custom file feedback</div> | ||
</div> | ||
</div> | ||
|
||
<div class="btn-group d-flex"> | ||
<%= form.submit 'Enviar', class: 'btn btn-success' %> | ||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<div> | ||
<h1 class="text-center">Editar imagem</h1> | ||
|
||
<%= render 'form', image: @image %> | ||
|
||
<hr> | ||
|
||
<div class="btn-group"> | ||
<%= link_to 'Detalhes', @image, class: 'btn btn-primary' %> | ||
<%= link_to 'Voltar', images_path, class: 'btn btn-secondary' %> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<p id="notice"><%= notice %></p> | ||
|
||
<h1>Images</h1> | ||
|
||
<table> | ||
<thead> | ||
<tr> | ||
<th>Title</th> | ||
<th>User</th> | ||
<th colspan="3"></th> | ||
</tr> | ||
</thead> | ||
|
||
<tbody> | ||
<% @images.each do |image| %> | ||
<tr> | ||
<td><%= image.title %></td> | ||
<td><%= image.user_id %></td> | ||
<td><%= link_to 'Show', image %></td> | ||
<td><%= link_to 'Edit', edit_image_path(image) %></td> | ||
<td><%= link_to 'Destroy', image, method: :delete, data: { confirm: 'Are you sure?' } %></td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
|
||
<br> | ||
|
||
<%= link_to 'New Image', new_image_path %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<div> | ||
<h1 class="text-center">Nova Imagem</h1> | ||
<%= render 'form', image: @image %> | ||
|
||
<hr> | ||
|
||
<%= link_to 'Voltar', images_path, class: 'btn btn-secondary' %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<p id="notice"><%= notice %></p> | ||
|
||
<p> | ||
<strong>Title:</strong> | ||
<%= @image.title %> | ||
</p> | ||
|
||
<p> | ||
<strong>User:</strong> | ||
<%= @image.user_id %> | ||
</p> | ||
|
||
<%= link_to 'Edit', edit_image_path(@image) %> | | ||
<%= link_to 'Back', images_path %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
Rails.application.routes.draw do | ||
resources :images | ||
devise_for :users | ||
root 'home#index', as: :home | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,15 @@ | ||
test: | ||
service: Disk | ||
root: <%= Rails.root.join("tmp/storage") %> | ||
|
||
local: | ||
service: Disk | ||
root: <%= Rails.root.join("storage") %> | ||
root: <%= Rails.root.join('storage') %> | ||
|
||
# Use rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key) | ||
# amazon: | ||
# service: S3 | ||
# access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %> | ||
# secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %> | ||
# region: us-east-1 | ||
# bucket: your_own_bucket | ||
pg: | ||
service: PostgreSQL | ||
|
||
# Remember not to checkin your GCS keyfile to a repository | ||
# google: | ||
# service: GCS | ||
# project: your_project | ||
# credentials: <%= Rails.root.join("path/to/gcs.keyfile") %> | ||
# bucket: your_own_bucket | ||
mirror: | ||
service: Mirror | ||
primary: local | ||
mirrors: [ pg ] | ||
|
||
# Use rails credentials:edit to set the Azure Storage secret (as azure_storage:storage_access_key) | ||
# microsoft: | ||
# service: AzureStorage | ||
# storage_account_name: your_account_name | ||
# storage_access_key: <%= Rails.application.credentials.dig(:azure_storage, :storage_access_key) %> | ||
# container: your_container_name | ||
|
||
# mirror: | ||
# service: Mirror | ||
# primary: local | ||
# mirrors: [ amazon, google, microsoft ] | ||
test: | ||
service: Disk | ||
root: <%= Rails.root.join('tmp/storage') %> |
36 changes: 36 additions & 0 deletions
36
db/migrate/20220126130616_create_active_storage_tables.active_storage.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# This migration comes from active_storage (originally 20170806125915) | ||
class CreateActiveStorageTables < ActiveRecord::Migration[5.2] | ||
def change | ||
create_table :active_storage_blobs 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 ], unique: true | ||
end | ||
|
||
create_table :active_storage_attachments do |t| | ||
t.string :name, null: false | ||
t.references :record, null: false, polymorphic: true, index: false | ||
t.references :blob, null: false | ||
|
||
t.datetime :created_at, null: false | ||
|
||
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 do |t| | ||
t.belongs_to :blob, null: false, index: false | ||
t.string :variation_digest, null: false | ||
|
||
t.index %i[ blob_id variation_digest ], name: "index_active_storage_variant_records_uniqueness", unique: true | ||
t.foreign_key :active_storage_blobs, column: :blob_id | ||
end | ||
end | ||
end |
11 changes: 11 additions & 0 deletions
11
...grate/20220126130623_create_active_storage_postgresql_tables.active_storage_postgresql.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# This migration comes from active_storage_postgresql (originally 20180530020601) | ||
class CreateActiveStoragePostgresqlTables < ActiveRecord::Migration[5.2] | ||
def change | ||
create_table :active_storage_postgresql_files do |t| | ||
t.oid :oid | ||
t.string :key | ||
|
||
t.index :key, unique: true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class CreateImages < ActiveRecord::Migration[6.1] | ||
def change | ||
create_table :images do |t| | ||
t.string :title | ||
t.references :user, null: false, foreign_key: true | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
Oops, something went wrong.