0

我正在尝试创建一个将 pdf 和图像上传到目录的表单。我目前有这个代码,我已经复制了一个工作示例:

def resources_addprocess

  r = Resource.new
  r.title = params[:title]
  r.reference = params[:reference]
  r.description = params[:description]
  r.campaign = params[:campaign]
  r.resourcetype = params[:resourcetype]

  uploaded_io = params[:file]
  File.open(Rails.root.join('public','resources', uploaded_io.original_filename), 'wb') do |file|
    file.write(uploaded_io.read)
  end
  if params[:preview].present?
    uploaded_io2 = params[:preview]
    File.open(Rails.root.join('app','assets','images','preview', 'resources', uploaded_io2.original_filename), 'wb') do |file|
      file.write(uploaded_io2.read)
    end
    r.preview = uploaded_io2.original_filename
  end
  r.file = uploaded_io.original_filename

  if r.save
    flash[:success] = "You successfully added a resource."
    redirect_to "/cms/resources"
  else
    flash[:error] = "resource wasn't successfully."
    redirect_to "/cms/resources"
  end
end

但我收到以下错误:undefined method 'original_filename'这适用于不同的项目..不知道为什么现在不是?

4

1 回答 1

0

发布后找到答案,

原来如果你使用=form_tag你需要定义它的多部分..

http://guides.rubyonrails.org/form_helpers.html#uploading-files

于 2013-05-31T11:53:59.950 回答