I've setup active storage on a rails project. I continuously get the error:
undefined method 'upload' for nil:NilClass
.
Here is what I've done.
rails active_storage:install
config.active_storage.service = :local
config.active_storage.service = :amazon
has_one_attached :image
My Controller:
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
Here is the form:
<%= simple_form_for(@event, url: events_path, method: :post) do |f| %>
<%= f.input :name %>
<%= f.input :image %>
<%= f.submit %>
<% end %>
Here is the model:
class Event < ActiveRecord::Base
has_one_attached :image
acts_as_list
end
It might be worth mentioning that I'm upgrading this particular project to rails 5. It also used to use paperclip.
The Problem: When I submit this form, the aforementioned error is thrown. The same error occurs when trying to update an @event object.
UPDATES
blob.upload io
. Whats weird is that when I debug this line, blob
is not nil. It holds an ActiveStorage::Blob
which doesn't have an id. And io
is not nil either.Error Logs:
I've taken out authenticity token purposely.
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
Explained. This is a common Ruby error which indicates that the method or attribute for an object you are trying to call on an object has not been defined.
1 What is Active Storage? Active Storage facilitates uploading files to a cloud storage service like Amazon S3, Google Cloud Storage, or Microsoft Azure Storage and attaching those files to Active Record objects.
You should add this line of code to your Gemfile:
gem 'aws-sdk-s3', require: false
Then add this other to your environments/production:
config.active_storage.service = :amazon # or wharever storage you are using...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With