简体   繁体   English

Rails 3和Paperclip问题-如何将上传的文件保存到文件系统?

[英]Rails 3 and Paperclip problem - How to save the uploaded file(s) to the filesystem?

I followed this tutorial trying to integrate Paperclip into my Rails 3 application. 我按照本教程尝试将Paperclip集成到我的Rails 3应用程序中。

However, my case looks a bit different from what is described in this tutorial. 但是,我的情况看起来与本教程中描述的有所不同。

In my case, User models are already exist in the database, and I want to upload file(s) and associate them with the uploader. 就我而言, User模型已经存在于数据库中,我想上传文件并将其与上传器关联。

Here are the relevant parts of my code: 这是我的代码的相关部分:

class User < ActiveRecord::Base
  has_many :assets, :foreign_key => "uploader_id"
end

class Asset < ActiveRecord::Base
  belongs_to :uploader, :class_name => "User"  
  has_attached_file :asset, :styles => { :thumb => "100x100#" }
end

The main difference between my case and the tutorial is that the upload input field is not inside User 's form: 我的案例和本教程之间的主要区别是上载输入字段不在User的表单内:

# views/lounge/index.html.erb
<%= form_tag('/lounge/upload', :multipart => true) do %>
  <input id="uploader_id" name="uploader_id" type="hidden" />
  <%= file_field_tag "assets[]", :multiple => true %>
<% end %>

The value of the hidden uploader_id input field is controlled by Javascript. 隐藏的uploader_id输入字段的值由Javascript控制。

When the form is submitted the upload method is called: 提交表单后, upload调用upload方法:

class LoungeController < ApplicationController
  def upload
    uploader = User.find(params[:uploader_id])
    # ??
  end
end

What should I do with params[:assets] in order to save the uploaded files to the filesystem and create the corresponding Asset models in the database ? 为了将上传的文件保存到文件系统并在数据库中创建相应的Asset模型,我该如何使用params[:assets]

To save the files, you'll need create the Asset models, and also assign the attached_file to those models (which you've also called asset , which could get confusing). 要保存文件,您需要创建Asset模型,并为这些模型分配Attached_file(您也将其称为asset ,这可能会引起混淆)。 In the simple case, this would just look like: 在简单的情况下,这看起来像:

user = User.find(id)
asset = user.assets.new
asset.asset = params[:asset]
asset.save

To do multiple files, simply do a quick loop: 要制作多个文件,只需执行一个快速循环:

user = User.find(id)
params[:assets].each do |asset|
  asset = user.assets.new
  asset.asset = asset
  asset.save
end

However, you're doing a number of non-standard things here, and making life harder for yourself than it needs to be. 但是,您在这里执行了许多非标准的操作,使自己的生活变得比实际需要更加艰辛。 Instead of using file_field_tag , you should be using the file_field helper with a Asset instantiated in memory. 而不是使用file_field_tag ,应该使用在内存中实例化资产的file_field帮助器。 Eg, if you followed the tutorial you linked to, you'd have something like: 例如,如果您遵循所链接的教程,则将出现以下内容:

<% form_for @user do |f|
  <% f.fields_for :assets do |asset| %>
    <%= asset.file_field :asset %>
  <% end %>
<% end %>

You'd also need to tell your User model that it's okay to accept child asset models when saving: 您还需要告诉您的用户模型,可以在保存时接受子资产模型:

def User
  accepts_nested_attributes_for :assets
end

Then in your controller action, you'd just build a few new assets in memory so that the loop in the fields_for works: 然后,在控制器操作中,您只需在内存中构建一些新资产,以使fields_for中的循环起作用:

def upload
  @user = User.find(id)
  5.times do { @user.assets.build }
end

Hope this makes sense. 希望这是有道理的。 Keep going - you'll get there. 继续前进-您会到达那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Rails Paperclip gem,如果模型无效,如何临时保存上传的文件 - With Rails Paperclip gem, how to temporarily save the uploaded file if the model is invalid 在Rails中,如何通过回形针将上传的文件(图像或pdf)保存到数据库? - In rails, how to save uploaded files (image or pdf) through paperclip to database? 如何在Rails 2中保存上传的文件 - How to save uploaded file in Rails 2 Rails,回形针无法保存文件 - Rails, Paperclip Failing to Save File Rails访问上载的文件并将其保存到PaperClip - Rails Accessing an Uploaded File and Saving it to PaperClip Rails回形针:不存储上传的文件 - Rails Paperclip: Do not store uploaded file 通过回形针上传文件时,Rails的未知格式 - Rails unknown format when file is uploaded by paperclip 使用Rails中的回形针将已经上传的文件上传到S3服务器,而无需下载该文件 - Uploading an already uploaded file to S3 server using paperclip in rails without downloading that file Rails 3 Paperclip Uploadify:在保存对象之前保存上传的对象附件 - Rails 3 Paperclip Uploadify : Save Uploaded Object Attachments Before Saving Object 在rails中使用rubzip2和回形针,如何创建一个临时zip文件并将其保存到回形针? - Using rubzip2 and paperclip in rails, how to you create a temporary zip file and save it to paperclip?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM