我正在使用 Paperclip 为 Ruby on Rails 编写一些图像上传代码,我有一个可行的解决方案,但它非常老套,所以我非常感谢有关如何更好地实现它的建议。我有一个“ Assets ”类,其中包含有关上传图像(包括回形针附件)的信息,以及一个封装尺寸信息的“生成器”类。每个“项目”都有多个 Assets 和生成器;所有 Assets 都应根据每个生成器指定的大小进行调整;因此,每个项目都有其所有 Assets 都应具有的一组特定大小。
发电机型号:
class Generator < ActiveRecord::Base
attr_accessible :height, :width
belongs_to :project
def sym
"#{self.width}x#{self.height}".to_sym
end
end
Assets 模型:
class Asset < ActiveRecord::Base
attr_accessible :filename,
:image # etc.
attr_accessor :generators
has_attached_file :image,
:styles => lambda { |a| a.instance.styles }
belongs_to :project
# this is utterly horrendous
def styles
s = {}
if @generators == nil
@generators = self.project.generators
end
@generators.each do |g|
s[g.sym] = "#{g.width}x#{g.height}"
end
s
end
end
Assets Controller 创建方法:
def create
@project = Project.find(params[:project_id])
@asset = Asset.new
@asset.generators = @project.generators
@asset.update_attributes(params[:asset])
@asset.project = @project
@asset.uploaded_by = current_user
respond_to do |format|
if @asset.save_(current_user)
@project.last_asset = @asset
@project.save
format.html { redirect_to project_asset_url(@asset.project, @asset), notice: 'Asset was successfully created.' }
format.json { render json: @asset, status: :created, location: @asset }
else
format.html { render action: "new" }
format.json { render json: @asset.errors, status: :unprocessable_entity }
end
end
end
我遇到的问题是先有鸡还是先有蛋的问题:新创建的 Assets 在正确实例化之前不知道要使用哪些生成器(大小规范)。我尝试使用 @project.assets.build,但在 Assets 获得其项目关联设置和 nils 之前,Paperclip 代码仍在执行。
'if @generators == nil' hack 是为了让更新方法无需在 Controller 中进行进一步的 hacking 即可工作。
总而言之,感觉很糟糕。任何人都可以建议如何以更明智的方式编写此代码,甚至可以建议采取这种方法吗?
提前致谢! :)
最佳答案
我在尝试使用基于具有多态关系的关联模型的动态样式的项目中遇到了同样的 Paperclip 鸡/蛋问题。我已将我的解决方案调整为您现有的代码。解释如下:
class Asset < ActiveRecord::Base
attr_accessible :image, :deferred_image
attr_writer :deferred_image
has_attached_file :image,
:styles => lambda { |a| a.instance.styles }
belongs_to :project
after_save :assign_deferred_image
def styles
project.generators.each_with_object({}) { |g, hsh| hsh[g.sym] = "#{g.width}x#{g.height}" }
end
private
def assign_deferred_image
if @deferred_image
self.image = @deferred_image
@deferred_image = nil
save!
end
end
end
基本上,要解决 Paperclip 在传播项目关系信息之前尝试检索动态样式的问题,您可以将所有 image
属性分配给非 Paperclip 属性(在这个实例,我将其命名为 deferred_image
)。 after_save
Hook 将 @deferred_image
的值分配给 self.image
,这启动了所有 Paperclip 爵士乐。
你的 Controller 变成:
# AssetsController
def create
@project = Project.find(params[:project_id])
@asset = @project.assets.build(params[:asset])
@asset.uploaded_by = current_user
respond_to do |format|
# all this is unrelated and can stay the same
end
end
和 View :
<%= form_for @asset do |f| %>
<%# other asset attributes %>
<%= f.label :deferred_upload %>
<%= f.file_field :deferred_upload %>
<%= f.submit %>
<% end %>
此解决方案还允许在 Project
模型中为 assets
关系使用 accepts_nested_attributes
(目前我正在使用它 - 以上传 Assets 作为创建/编辑项目的一部分)。
这种方法有一些缺点(例如,根据 Asset
实例的有效性验证 Paperclip image
变得棘手),但这是我能做的最好的想出猴子修补 Paperclip 以某种方式推迟 style
方法的执行,直到关联信息被填充之后。
我会一直关注这个问题,看看有没有人有更好的办法解决这个问题!
至少,如果您选择继续使用相同的解决方案,则可以对 Asset#styles
方法进行以下风格改进:
def styles
(@generators || project.generators).each_with_object({}) { |g, hsh| hsh[g.sym] = "#{g.width}x#{g.height}" }
end
与您现有的方法完全相同,但更简洁。
关于ruby-on-rails - Ruby on Rails - 回形针和动态参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14305018/