Remove question mark from Paperclip-generated files in Ruby on Rails 3.2.6

扶醉桌前 提交于 2019-12-04 05:10:12

If you want to do this everywhere for a given attachment and without the need to pass the extra parameter, you can set the use_timestamp option when calling the has_attached_file method in your model. So, to build on the example given in the Paperclip README:

has_attached_file :avatar,
  :styles => { :medium => "300x300>", :thumb => "100x100>" },
  :default_url => "/images/:style/missing.png",
  :use_timestamp => false

Hope this is OK to put as an answer to my own question (as it may be useful for others who stumble across this post), but I've since discovered that an alternative (and more appropriate) way to deal with this issue is to add the false parameter to URL() as follows when displaying the content in your view:

model.paperclipattribute.url(:whateverstyle, false)

The timestamp will automatically be removed. I think this is better, as the split method I suggested might remove content you don't intend to remove - for example, if your file is called something like "Is_this_a_question_?_Yes_it_is.mp4?xxxxxx", then everything after the first question mark might be removed (i.e. the file will be read as "Is this a question_", thus corrupting the filename.

I haven't tested this, so I may be wrong.

Globally default them to off, just put this in a config/initializers/paperclip.rb file.

Paperclip::Attachment.default_options[:use_timestamp] = false

You want to use split instead I believe. strip doesn't take any argument, it just removes leading and trailing whitespaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!