Carrierwave没有将文件保存在磁盘上

时间:2014-10-18 08:31:35

标签: ruby-on-rails ruby-on-rails-4 carrierwave

大家。请帮助我使用carrierwave和Rails 4。

型号:

class MediaItem < ActiveRecord::Base
  mount_uploader :media_item, MediaItemUploader
end

架构:

create_table "media_items", force: true do |t|
  t.string   "file"
  t.text     "description", default: "", null: false
  t.datetime "created_at",               null: false
  t.datetime "updated_at",               null: false
end

者:

class MediaItemUploader < CarrierWave::Uploader::Base
  def store_dir
    "public/"
  end

控制器:

class MediaItemsController < ApplicationController
  def create
    @media_item = MediaItem.new(media_item_params)

    respond_to do |format|
      if @media_item.save
        format.html { redirect_to @media_item, notice: 'Media item was successfully created.' }
      else
        format.html { render :new }
      end
    end
  end

形式:

= form_for @media_item, :html => {:multipart => true} do |f|
  .field
    = f.label :description
    = f.text_field :description
  .field
    = f.label :file
    = f.file_field :file
  .actions
    = f.submit 'Save'

问题是上传的文件没有出现在指定的目录和&#34; media_item.media_item_url&#34;在视图中返回空字符串。但是,它将recore保存到db:

select file, description from media_items;
#<ActionDispatch::Http::UploadedFile:0xba672374>|wetw

1 个答案:

答案 0 :(得分:1)

我的错误。

相反

mount_uploader :media_item, MediaItemUploader

一定是

mount_uploader :file, MediaItemUploader

as“file”是db中的字段名,而不是“media_item”