0

次のエラーが表示されます。

undefined method `each' for nil:NilClass

ここ:

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

これは私が移入する場所です@images

class Users::RegistrationsController < Devise::RegistrationsController

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

私のアップロードフォーム:

  <%= 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 %>

ペーパークリップの検証に失敗した場合にのみこのエラーが発生します(画像のように> 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

問題は、ファイルのアップロードの失敗がどのように処理されることになっているのかわかりません。

4

1 に答える 1

0

ファイルクラスを使用して、ファイルが存在するかどうかを確認し、「ファイル」がディレクトリではなく本当にファイルであるかどうかを確認します。また、データベース フィールドが null でないことを確認して確認し、そうである場合は (後で削除するために) レコードにマークを付けます。

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

これは Sidekiq ワーカー (バックグラウンド ジョブ) の一部として行います。複数のアップロードをチェックして、ジョブの一部として関連オブジェクトを更新しています。

于 2015-03-14T00:18:03.307 に答える