对于#<actiondispatch :: http :: uploadedfile:0xa5741c0>

时间:2015-04-30 13:40:45

标签: ruby-on-rails ruby-on-rails-4 carrierwave image-uploading nested-attributes

我尝试使用CarrierWave上传文件,而Image模型是多态的。但我得到了标题中的错误,所以这是我的模型:

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

和_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>

和service_controller“创建”操作:

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"=>""}

那么,在这种情况下出现了什么问题?

1 个答案:

答案 0 :(得分:3)

您正在尝试遍历图像数组,但您的参数只有一个图像,因此修复已被替换:

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

@image = @service.images.create!(:image => params[:images][:image], :imageable_id => @service.id, :image_type => params[:images][:image_type])