0

I get this error:

undefined method `each' for nil:NilClass

Here:

<% @images.each do |image| %>

This is where I populate @images:

class Users::RegistrationsController < Devise::RegistrationsController

  def edit
    @images= Dir.glob("public/assets/images/users/#{current_user.id}/med/*")
    super
  end
  ...

My upload form:

  <%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: {:multipart => true }) do |f| %>
    <div class="form-group">
      <label for = 'file-upload', class='btn btn-large btn-block btn-info'>
        <span class="glyphicon glyphicon-upload" aria-hidden="true"></span> Upload Picture
        <%= f.file_field :image, id: "file-upload", onchange: "this.form.submit()" %>
      </label>
    </div>
  <% end %>

I only get this error when i fail paperclip validation (as in image > 1mb)

    has_attached_file :image, {styles: { small: "24x24#", med: "100x100#", large: "200x200#" },
                  :url  => "/assets/images/users/:id/:style/:basename.:extension",
                  :path => ":rails_root/public/assets/images/users/:id/:style/:basename.:extension",
                  :default_url => "/assets/images/users/default/:style/default.png",
                  :keep_old_files => true}

    validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
    validates_attachment_size :image, :less_than => 1.megabytes

The thing is that I don't see how the file upload fail is supposed to be handled.

Vlad Otrocol
  • 2,952
  • 7
  • 33
  • 55
  • Put a simple if condition that '<%if @images.present? %> <% @images.each do |image| %><%end%><%end%> ' – Arvind Mar 14 '15 at 06:29
  • but `@images` should get the paths of all my images in that folder. Even if my upload fails I should still be able to retrieve all the other image paths into my `@images` array – Vlad Otrocol Mar 14 '15 at 09:31
  • 1
    But what error you have mention is not related to that , error is occurring due to each action in nil object. so first you need to control this. – Arvind Mar 14 '15 at 13:22
  • you are right @Arv make this and answer and I accept it – Vlad Otrocol Mar 14 '15 at 13:23
  • meanwhile check this please: http://stackoverflow.com/questions/29049002/why-does-devise-me-redirect-me-to-users-instead-of-users-edit-when-my-account/29049050#29049050 – Vlad Otrocol Mar 14 '15 at 13:24

1 Answers1

0

I use the file class and check if the file exists and check if the 'file' is truly a file and not a directory. I also check and make sure that database field is not null and mark the record (for deletion later) if it is.

File.exist?('image.jpg')  && File.file?('image.jpg')

I do this as part of a Sidekiq Worker (Background Job), I am checking multiple uploads updating a related object as part of the job.

Ryan Condron
  • 429
  • 1
  • 3
  • 14