[英]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.