Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does active storage throw this error: undefined method `upload' for nil:NilClass, when object is not nil

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.

  • I've ran the migration: rails active_storage:install
  • I've edited my development environtment file: config.active_storage.service = :local
  • Same with my production file: config.active_storage.service = :amazon
  • Ive made sure that my model is setup correctly with 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

  • when looking through framework trace, the error is being thrown on this line: 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
like image 469
Angel Garcia Avatar asked Aug 23 '18 15:08

Angel Garcia


People also ask

What does it mean when you get this error message undefined method user for Nilclass?

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.

What is ActiveStorage?

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.


1 Answers

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...
like image 161
Amarildo Lucas Avatar answered Nov 15 '22 09:11

Amarildo Lucas