扩展 Paperclip

Paperclip 是 Rails 的一个处理 attachment 的插件,相对于以往的 FileColumn 在灵活性和效率上更胜一筹,而且代码也比较好看。这个视频 简单的介绍了 Paperclip 的使用方法。

默认的设置,URL 的占位符中与模型本身相关的只有 id,但是一些情况下,你可能会更希望以其他形式来组织你的附件目录 - 比如以 SKU 来代替数据库记录的 id。这里我们暂不讨论这种做法的好坏,双方面的,好处是目录结构更具有维护性,坏处是万一以后升级数据库,SKU 加个前缀什么的…

Here we go!

使用 paperclip 需要在 model 中调用 has_attached_file 方法,检查文档,有一些简单的使用样例,但是没有我们需要的。通过方法描述我们知道这个方法建立了一个 Paperclip::Attachment 对象,我们继续看文档。对象的方法很少,首先思考:应为我们需要配置的是 attachment 的 url 规则,那么应当是对应整个类而不是单个实力,因此我们只关注 Peperclip::Attachment 的类方法,只有两个。default_options 没有描述,而且展开代码发现并不是我们需要的。

引用
# Paperclip::Attchment.interpolation
A hash of procs that are run during the interpolation of a path or
url. A variable of the format :name will be replaced with the return
value of the proc named “:name”. Each lambda takes the attachment and
the current style as arguments. This hash can be added to with your own
proc if necessary.

这正是我们需要的,接下来的扩展就非常方便了:

# app/models/product.rb
class Product < ActiveRecord::Base
  has_attached_file :photo,
    :style => { :thumb => '64x64>' },
    :url => '/images/products/:to_param.:extension'
  
  def to_param
    return self.sku
  end
end

# config/initializers/paperclip.rb
Paperclip::Attachment.interpolations.merge!(
  :to_param => lambda { |attachment, style| attachment.instance.to_param }
)

在这里不直接使用 :sku 作为占位符而使用 :to_param 是为了在其他模型中更加的灵活。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<div v-else-if="per.dict_value === 'file_name'"> <el-tooltip placement="top" effect="dark" v-if="scope.row.download_time || scope.row.repair_download_time"> <div slot="content" > <span v-if="scope.row.repair_download_time">{{ parseTime(scope.row.repair_download_time,'{y}-{m}-{d} {h}:{i}:{s}') }}</span> <span v-else-if="scope.row.download_time">{{ parseTime(scope.row.download_time,'{y}-{m}-{d} {h}:{i}:{s}') }}</span> </div> <el-link v-if="scope.row.repair_file_id" :key="scope.row.repair_file_id" style="display: block;color: #67C23A" icon="el-icon-paperclip" @click="downloadOrderExcel(scope.row.repair_file_id, scope.row.order_no, scope.row.repair_file_name)" >{{ scope.row.repair_file_name }}</el-link> <el-link v-else-if="scope.row.file_id" :key="scope.row.file_id" style="display: block;color: #67C23A" icon="el-icon-paperclip" @click="downloadOrderExcel(scope.row.file_id, scope.row.order_no, scope.row.file_name)" >{{ scope.row[per.dict_value] }}</el-link> </el-tooltip> <div v-else> <el-link v-if="scope.row.repair_file_id" :key="scope.row.repair_file_id" style="display: block;" icon="el-icon-paperclip" @click="downloadOrderExcel(scope.row.repair_file_id, scope.row.order_no, scope.row.repair_file_name)" >{{ scope.row.repair_file_name }}</el-link> <el-link v-else-if="scope.row.file_id" :key="scope.row.file_id" style="display: block;" icon="el-icon-paperclip" @click="downloadOrderExcel(scope.row.file_id, scope.row.order_no, scope.row.file_name)" >{{ scope.row[per.dict_value] }}</el-link> </div> </div>优化这段代码用vue2.0
06-07

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值