Search code examples
ruby-on-railsruby-on-rails-5rails-activestorage

ActiveStorage: NoMethodError (undefined method `[]' for nil:NilClass)


I have been investigating this issue for the past 2 days, I am running rails 5.3.2 and i have been trying to use active storage for a profile picture upload of users that are already created

My model looks like this:

MyModel < ApplicationRecord
  has_one_attached :avatar
  ...
end

my controller:

class MyModelsController < ApplicationController
  before_action :load_my_model, only: %i[edit show update]
  ...
  def update
    authorize @my_model

    @my_model.avatar.attach(params[:my_model][:avatar])

    respond_to do |format|
      if @my_model.update(model_params)
        format.html { redirect_to my_model_url(@my_model), notice: 'My model was successfully updated.' }
        format.json { render :show, status: :ok, location: @my_model }
      else
        format.html { render :edit }
        format.json { render json: @my_model.errors, status: :unprocessable_entity }
      end
    end
  end
  ...
  private

  def load_my_model
    @my_model = MyModel.find(params[:id])
  end
 end

it throws an error everytime on this line:

 @my_model.avatar.attach(params[:my_model][:avatar])

The Error:

NoMethodError (undefined method `[]' for nil:NilClass):

(erb):19:in `<main>'
app/controllers/my_models_controller.rb:38:in `block in update'
app/controllers/my_models_controller.rb:37:in `update'

The raw Parameters:

{
"utf8"=>"✓",
"authenticity_token"=>"*******REDACTED****", 
"my_model"=>
 {
  "name"=>"Bambam",
  "my_model_type"=>"",
  "country"=>"blue",
  "avatar"=>#<ActionDispatch::Http::UploadedFile:0x00007fdd275ce3e0 @tempfile=#<Tempfile:/var/folders/9p/llb8yydx59g535nqkbj0xv0w0000gp/T/RackMultipart20200201-7622-vp74wb.png>, @original_filename="undraw_instant_information_6755.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"hair_stylist[avatar]\"; filename=\"undraw_instant_information_6755.png\"\r\nContent-Type: image/png\r\n">
 },
 "commit"=>"Update",
 "id"=>"2"
}

I tried:

  • Reinstalling activestorage
  • Moving it to different models and forms to see if it works and same issue
  • I tried removing that line and just use: @my_model.update(model_params) to update and it throws the same error

I am really running out of ideas, any idea of what the issue is?


Solution

  • There is a issue related to rails credentials. if you just followed the active storage guide you might have configured a storage service provider at config/storage.yml. Regardless your using it as development storage service it will raise this exception when attaching files. For instance:

    amazon:
      service: S3
        access_key_id: <%= Rails.application.credentials[Rails.env.to_sym][:aws][:access_key_id] %>
        secret_access_key: <%= Rails.application.credentials[Rails.env.to_sym][:aws][:secret_access_key] %>
        region: us-east-1
        bucket: ...
    

    So make sure you have rails credentials set, or change it to your environment variables.