Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined method `each' for #<ActionDispatch::Http::UploadedFile:0xa5741c0>

I try to upload a file, with using CarrierWave, and the Image model is polymorphic. But i got the error as in the title, so here is my models:

class Image < ActiveRecord::Base
    belongs_to :imageable, polymorphic: true

    mount_uploader :image, ImageUploader

    enum image_type: {icon: 1, title: 2, regular: 3, slider: 4}
end

class Service < ActiveRecord::Base
    has_many :images, as: :imageable, :dependent => :destroy
    accepts_nested_attributes_for :images

end

and the _form.html.erb :

<div class="control-group">
   <%= simple_fields_for :images do |image| %>


     <div class="controls">
        <label class="control-label">Hizmet Büyük Resimi</label> 
        <div class="fileupload fileupload-new" data-provides="fileupload">
          <div class="fileupload-new thumbnail" style="width: 200px; height: 150px;"><img src="http://www.placehold.it/200x150/EFEFEF/AAAAAA&amp;text=no+image" alt=""/></div>
          <div class="fileupload-preview fileupload-exists thumbnail" style="max-width: 200px; max-height: 150px; line-height: 20px;"></div>
          <div>
            <span class="btn btn-file"><span class="fileupload-new">Resim Seç</span><span class="fileupload-exists">Değiştir</span>
              <%= image.input :image, input_html: { type: :file, class: "default"}, wrapper: false, label: false %>
              <%= image.input :image_type, :as => "hidden", :input_html => { :value => 2 } %>
            </span>
            <a href="#" class="btn fileupload-exists" data-dismiss="fileupload">Sil</a>
          </div>
        </div>


     </div>
   <% end %>
</div>

and the service_controller "create" action:

def create
    @service = Service.new(service_params)

    if @service.save
      if params[:images]
        params[:images][:image].each do |i|
          @image = @service.images.create!(:image => i, :imageable_id => @service.id, :image_type => params[i][:image_type])
        end
      end

      flash[:notice] = "Hizmet sayfası başarıyla oluşturuldu!"
      redirect_to admin_services_path
    else
      flash[:alert] = "Hizmet sayfası eklenirken bir hata oluştu. Lütfen tekrar deneyiniz!"
      render "new"
    end
  end

ErrorMessage:

undefined method `each' for #<ActionDispatch::Http::UploadedFile:0xa5741c0>

Rails.root: D:/Projects/TorkGarage/website/torkgarage

Application Trace | Framework Trace | Full Trace
app/controllers/admin/services_controller.rb:19:in `create'
Request

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"yN62eOMu2gTrNIWgs27XBiA3uxIAGVgTGt4ywTSm3lKn4kEqG2Ka+3IdaqvSpRQrOPVkBBiuWLl1l5jx5fEUWw==",
 "service"=>{"service_name"=>"Rot - Balans",
 "service_description"=>"sdsad asdas&nbsp;",
 "meta_keywords"=>"adas da"},
 "_wysihtml5_mode"=>"1",
 "images"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0xa5741c0 @tempfile=#<Tempfile:C:/Users/mehmet/AppData/Local/Temp/RackMultipart20150430-6992-8dc0q8.png>,
 @original_filename="Layer-6.png",
 @content_type="image/png",
 @headers="Content-Disposition: form-data; name=\"images[image]\"; filename=\"Layer-6.png\"\r\nContent-Type: image/png\r\n">,
 "image_type"=>"2"},
 "button"=>""}

So, what's going on wrong in this situation?

like image 726
mehmetserif Avatar asked Oct 19 '22 12:10

mehmetserif


1 Answers

You are trying to iterate through an array of images but your params only have one image so the fix is replace:

params[:images][:image].each do |i|
  @image = @service.images.create!(:image => i, :imageable_id => @service.id, :image_type => params[i][:image_type])
end

with

@image = @service.images.create!(:image => params[:images][:image], :imageable_id => @service.id, :image_type => params[:images][:image_type])
like image 127
Aguardientico Avatar answered Oct 22 '22 03:10

Aguardientico