尝试附加具有Active Storage错误的文件

时间:2019-01-29 06:16:55

标签: ruby-on-rails ruby postgresql rails-activestorage

我正在尝试使用Active Storage附加文件并不断出现错误:

无法自动加载常量ActiveStorage :: Blob :: Analyzable,预期 /users/sergeylukyanenko/.rvm/gems/ruby-2.6.0/gems/activestorage-5.2.2/app/models/active_storage/blob/analyzable.rb进行定义

Request
Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"L0czdSokZuL6CyRqMNEUAj/6AGRItCOZ/oIca6OGHtgsxD+ExTnZ1BtMdvFyHQTOEZ5/5LEd6dma23r1r8zIlA==",
 "event"=>
  {"name"=>"Coffee meetup",
   "date"=>"2019-01-28",
   "image"=>
    #<ActionDispatch::Http::UploadedFile:0x00007ff464e03278
     @content_type="image/jpeg",
     @headers="Content-Disposition: form-data; name=\"event[image]\"; filename=\"agile-software-development.jpg\"\r\n" + "Content-Type: image/jpeg\r\n",
     @original_filename="agile-software-development.jpg",
     @tempfile=#<File:/var/folders/6j/xkzm5glx2k9_ycr6c235lx2m0000gn/T/RackMultipart20190128-91451-1jdmov.jpg>>},
 "commit"=>"Create Event"}

这是我的EventsController

def create
    if current_user
      @event = current_user.events.build(event_params)
    else
      @event = Event.new(event_params)
    end

    respond_to do |format|
      if @event.save
        format.html { redirect_to @event, :flash => { :success => 'Event was successfully created.' }}
        format.json { render :show, status: :created, location: @event }
      else
        format.html { render :new }
        format.json { render json: @event.errors, status: :unprocessable_entity }
      end
    end
  end

  def event_params
    params.require(:event).permit(:name, :date, :private, :image)
  end

事件模型

class Event < ApplicationRecord
  validates :name, presence: true
  validates :date, presence: true

  has_one_attached :image
  belongs_to :user, optional: true
  has_many :tasks, dependent: :destroy

  scope :only_public,    -> { where(private: false) }
  scope :only_private,   -> { where(private: true)  }
  scope :by_recent,      -> { order(date: :asc) }

  def start_time
    self.date
  end
end

活动表单

<div class="field mt-4">
  <%= form.file_field :image %>
</div>

config / storage.yml

test:
  service: Disk
  root: <%= Rails.root.join("tmp/storage") %>

local:
  service: Disk
  root: <%= Rails.root.join("storage") %>

# Use rails secrets:edit to set the AWS secrets (as shared:aws:access_key_id|secret_access_key)
amazon:
  service: S3
  access_key_id: <%= Rails.application.secrets.aws[:access_key_id] %>
  secret_access_key: <%= Rails.application.secrets.aws[:secret_access_key] %>
  region: us-east-1
  bucket: your_own_bucket

1 个答案:

答案 0 :(得分:0)

选中ActiveStorage installation

请运行以下命令来为ActiveStorage生成迁移。

rails active_storage:install

然后

rails db:migrate

编辑:setup doc

所述