2014-09-01 84 views
0

我試圖用carrierwave將它保存上傳的圖像,目前僅存「空」的相關行發送的PARAMATERS,任何有識之士將appricated:Ruby on Rails的 - Carrierwave不保存文件

我「M使用rails-apihttps://github.com/rails-api/rails-api

#controller 
def create 
    @picture = Picture.new(picture_params) 

    if @picture.save 
    render json: @picture, status: :created, location: @picture 
    else 
    render json: @picture.errors, status: :unprocessable_entity 
    end 
end 

class Picture < ActiveRecord::Base 
    belongs_to :imageable, polymorphic: true 
    mount_uploader :image_url, ImageUploader 
end 

class ImageUploader < CarrierWave::Uploader::Base 
    storage :file 
    def store_dir 
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 
    end 
end 

架構

create_table "pictures", force: true do |t| 
    t.string "image_url" 
    t.integer "imageable_id" 
    t.string "imageable_type" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
end 

控制檯輸出

Started POST "/pictures" for 127.0.0.1 at 2014-09-01 23:44:47 +0100 
Processing by PicturesController#create as */* 
Parameters: {"test.jpg"=>#<ActionDispatch::Http::UploadedFile:0x2591868 @tempfile=# <File:C:/Users/xxx/AppData/Local/Temp/RackMultipart20140901-5044-1eui2xj>,  @original_filename="test.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\" 
test.jpg\"; filename=\"test.jpg\"\r\nContent-Type: image/jpeg\r\n">} 
←[1m←[36m (0.0ms)←[0m ←[1mbegin transaction←[0m 
←[1m←[35mSQL (1.0ms)←[0m INSERT INTO "pictures" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2014-09-01 22:44:47.289649"], ["updated_at", "2014-09-01 22:44:47.289649"]] 
←[1m←[36m (509.0ms)←[0m ←[1mcommit transaction←[0m 
Completed 201 Created in 514ms (Views: 2.0ms | ActiveRecord: 510.0ms) 

以上POST不會發送imageable_id或類型,但是將這些屬性添加到帖子中並不能解決問題。

編輯:添加下面的控制器允許其保存等領域,但carrierwave還沒有保存iamge地址

def picture_params 
    params.require(:picture).permit(:imageable_id, :imageable_type, :image_url) 
end 

回答

0

我的工作了:我用的是郵遞員,需要列出標題爲picture[image_url],不只是名字。