我已经在Rails项目上设置了活动存储。我不断收到错误:
undefined method 'upload' for nil:NilClass
。
这就是我所做的。
rails active_storage:install
config.active_storage.service = :local
config.active_storage.service = :amazon
has_one_attached :image
正确设置了我的模型我的控制器:
class EventsController < ApplicationController
def create
@event = Event.new(event_params)
@event.save
end
private
def event_params
params.require(:event).permit(:name, :image)
end
end
这是表格:
<%= simple_form_for(@event, url: events_path, method: :post) do |f| %>
<%= f.input :name %>
<%= f.input :image %>
<%= f.submit %>
<% end %>
这是模型:
class Event < ActiveRecord::Base
has_one_attached :image
acts_as_list
end
值得一提的是,我正在将该特定项目升级到Rails5。它也曾经使用回形针。
问题: 当我提交此表单时,将引发上述错误。相同 尝试更新@event对象时发生错误。
更新
blob.upload io
。奇怪的是,当我调试此行时,blob
不是nil。它包含一个ActiveStorage::Blob
,其中没有ID。 io
也不为零。错误日志:
我故意取出了真实性令牌。
Started POST "/events" for ::1 at 2018-08-23 10:02:25 -0600
Processing by EventsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"*****************", "event"=>{"name"=>"test event", "image"=>#<ActionDispatch::Http::UploadedFile:0x00007fc816e90af8 @tempfile=#<Tempfile:/var/folders/f9/q3x5477d1dxd7fxjbrl2tvch0000gn/T/RackMultipart20180823-61111-14dih2d.jpg>, @original_filename="sample-image.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"event[image]\"; filename=\"sample-image.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Update Event"}
Completed 500 Internal Server Error in 61ms (ActiveRecord: 0.0ms)
NoMethodError (undefined method `upload' for nil:NilClass):
app/controllers/events_controller.rb:4:in `create'
::1 - - [23/Aug/2018:10:02:25 MDT] "POST /events HTTP/1.1" 500 138644
http://localhost:3000/ -> /events
答案 0 :(得分:1)
您应将以下代码行添加到Gemfile中:
gem 'aws-sdk-s3', require: false
然后将其他添加到您的环境/生产中
config.active_storage.service = :amazon # or wharever storage you are using...