Как превратить атрибут с помощью Active Storage OPTIONAL (AWS S3 и ActiveStorage) - PullRequest
0 голосов
/ 12 февраля 2020

Я создаю приложение, используя activestorage и AWS S3 для хранения файлов. Я хочу, чтобы аватар в этом приложении был необязательным, однако после настройки AWS S3 и установки на моей модели пользователя «has_one_attached: avatar» я понял, что атрибут аватара включен, обязательный.

rails route:

Rails.application.routes.draw do
  devise_for :users, defaults: { format: :json }
end

Модель пользователя:

class User < ApplicationRecord
  has_many :companies
  has_one_attached :photo
end

require "application_responder"

class ApplicationController < ActionController::API
self.responder = ApplicationResponder
respond_to :html, :json

before_action :configure_permitted_parameters, if: :devise_controller?
before_action :configure_account_update_params, only: [:update]
before_action :authenticate_user!, only: [:update]
before_action :configure_sign_in_params, only: [:create]
  
include Pundit

# Pundit: white-list approach.
after_action :verify_authorized, except: :index, unless: :skip_pundit?
after_action :verify_policy_scoped, only: :index, unless: :skip_pundit?

# Uncomment when you *really understand* Pundit!
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
def user_not_authorized
flash[:alert] = "You are not authorized to perform this action."
redirect_to(root_path)
end

private

def skip_pundit?
devise_controller? || params[:controller] =~ /(^(rails_)?admin)|(^pages$)/
end

protected

def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:cpf, :full_name, :phone_number, :password, :password_confirmation])
end

def configure_account_update_params
devise_parameter_sanitizer.permit(:account_update, keys: [
:full_name, :phone_number, :photo, :nick_name, :email, :current_password, :password, :password_confirmation])
end

def configure_sign_in_params
devise_parameter_sanitizer.permit(:sign_in, keys: [:login])
end

Application Controller:

Я уже прочитал документы по активному хранилищу, и там пока нет решения. https://guides.rubyonrails.org/active_storage_overview.html

Application:
ruby 2.6.3
Rails 6.0.2.1
Active storage: I already run rails active_storage:install
rails db:migrate

Фактическое поведение: Если я пытаюсь создать пользователя без аватара, в конце сообщения появляется ошибка 500.

Started POST "/users" for ::1 at 2020-02-17 21:22:30 -0300
Processing by Devise::RegistrationsController#create as JSON
  Parameters: {"user"=>{"cpf"=>"75816927019", "password"=>"[FILTERED]", "phone_number"=>"79989972620", "full_name"=>"Mario de Andrade"}, "registration"=>{"user"=>{"cpf"=>"75816927019", "password"=>"[FILTERED]", "phone_number"=>"79989972620", "full_name"=>"Mario de Andrade"}}}
   (0.2ms)  BEGIN
  User Exists? (1.4ms)  SELECT 1 AS one FROM "users" WHERE "users"."phone_number" = $1 LIMIT $2  [["phone_number", "79989972620"], ["LIMIT", 1]]
  User Exists? (0.3ms)  SELECT 1 AS one FROM "users" WHERE "users"."cpf" = $1 LIMIT $2  [["cpf", "75816927019"], ["LIMIT", 1]]
   (0.8ms)  SELECT COUNT(*) FROM "users" WHERE "users"."authentication_token" = $1  [["authentication_token", "fS3SvuYXgH4B89n-khRX"]]
  User Create (0.6ms)  INSERT INTO "users" ("encrypted_password", "created_at", "updated_at", "status", "phone_number", "cpf", "full_name", "authentication_token") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"  [["encrypted_password", "$2a$11$gi3ee1iRFRzzJzdPHHUvZ.rDsWGRlqBTU6aGtWylCBbK2NwwfUCim"], ["created_at", "2020-02-18 00:22:31.368473"], ["updated_at", "2020-02-18 00:22:31.368473"], ["status", "Ativo"], ["phone_number", "79989972620"], ["cpf", "75816927019"], ["full_name", "Mario de Andrade"], ["authentication_token", "fS3SvuYXgH4B89n-khRX"]]
   (2.4ms)  COMMIT
  User Load (0.8ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 23], ["LIMIT", 1]]
  ActiveStorage::Attachment Load (0.7ms)  SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4  [["record_id", 26], ["record_type", "User"], ["name", "photo"], ["LIMIT", 1]]
Completed 500 Internal Server Error in 508ms (ActiveRecord: 31.3ms | Allocations: 68570)


  
SystemStackError (stack level too deep):
  
activestorage (6.0.2.1) lib/active_storage/attached/one.rb:13:in `public_send'
activestorage (6.0.2.1) lib/active_storage/attached/one.rb:13:in `attachment'
activesupport (6.0.2.1) lib/active_support/core_ext/module/delegation.rb:293:in `respond_to_missing?'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:54:in `respond_to?'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:54:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:57:in `as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `block in as_json'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `each'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `map'
activesupport (6.0.2.1) lib/active_support/core_ext/object/json.rb:172:in `as_json'
activemodel (6.0.2.1) lib/active_model/serializers/json.rb:96:in `as_json'
activesupport (6.0.2.1)

Желаемое поведение: Создайте нового пользователя без необходимости загружать: avatar, однако, если пользователь решает загрузить: avatar, используйте activestorage для отправки и хранилище на AWS S3.

Кто-нибудь может мне помочь настроить это поведение?

...