我正在尝试使用ActiveStorage和aws s3替换带有图像上传的折旧(对于Rails 5.2)回形针.我正在使用本指南和GoRails剧集,但遇到了我无法解析的控制台错误.在我的表单上看起来一切都很好,但是当我去提交时,我会在我的控制台中看到它:
activestorage.self-6e8d967adecc8b2a7259df0f51ef5b6f171c33267c7d149a474beccd90c68697.js?body=1:1 POST http://localhost:3000/rails/active_storage/direct_uploads 500 (Internal Server Error)
Run Code Online (Sandbox Code Playgroud)
我在blogs#new_ 上有这个form.html.erb:
<div class="form-group">
<%= f.label "Picture" %>
<%= f.file_field :image, direct_upload: true %>
</div>
Run Code Online (Sandbox Code Playgroud)
我把它放在我的模型中:
has_one_attached :image
Run Code Online (Sandbox Code Playgroud)
我有:image我的参数
def blog_params
params.require(:blog).permit(:title, :teaser, :body, :user_id, :tag_list, :link_text, :link_filename, :pillars_id, :image)
end
Run Code Online (Sandbox Code Playgroud)
我把它添加到我的storage.yml:
Use rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key)
amazon:
service: S3
access_key_id: <%= ENV["AWS_ACCESS_KEY_ID"] %>
secret_access_key: <%= ENV["AWS_SECRET_ACCESS_KEY"] %>
region: us-west-1
bucket: nameofbucket
Run Code Online (Sandbox Code Playgroud)
这在我的开发环境中:
config.active_storage.service = …Run Code Online (Sandbox Code Playgroud) 我有:
设置 Rails 项目
添加了活动管理员。
设置 Active Storage,更新配置以进行本地连接并执行以下操作:
bin/rails active_storage:安装
bin/rails db:migrate
然后,当尝试建立一个Document带有附加文件的非常简单的模型时:
class Document < ApplicationRecord
has_one_attached :file
end
Run Code Online (Sandbox Code Playgroud)
最简单的活动管理页面可能是这样的:
ActiveAdmin.register Document do
permit_params :name, :type, :file
end
Run Code Online (Sandbox Code Playgroud)
我在尝试访问该页面时收到以下错误:
Rails 找不到 ActiveStorage::Blob 关联的有效模型。请在关联声明中提供 :class_name 选项。如果 :class_name 已提供,请确保它是 ActiveRecord::Base 子类。
我不确定出了什么问题。从文档看来,迁移并将该行添加到模型后一切都应该正常工作。
Ruby 版本:“3.1.1”
Rails 版本:“7.0.2”
我刚刚升级了5.1.4。应用程式至5.2,并尝试将Paperclip换成ActiveStorage。在尝试使用图像更新现有记录时,出现以下错误:
无法自动加载常量ActiveStorage :: Blob :: Analyzable,需要/Users/Simon/.rvm/gems/ruby-2.4.0/gems/activestorage-5.2.0/app/models/active_storage/blob/analyzable.rb进行定义它
在我的模型中:
has_one_attached :pic
Run Code Online (Sandbox Code Playgroud)
在我的控制器中:
...
def update
respond_to do |format|
if @gin.update(gin_params)
format.html { redirect_to @gin, notice: 'Gin was successfully updated.' }
format.json { render :show, status: :ok, location: @gin }
else
format.html { render :edit }
format.json { render json: @gin.errors, status: :unprocessable_entity }
end
end
end
Run Code Online (Sandbox Code Playgroud)
...
def gin_params params.require(:gin).permit(:name, :text, :snippet,
:pic, :slug, :abv, distillery_attributes: [:id, :name], botanical_ids:
[]) end
Run Code Online (Sandbox Code Playgroud)
在storage.yml中:
amazon:
service: S3
access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %>
secret_access_key: <%= …Run Code Online (Sandbox Code Playgroud)