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

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-21 12:06:59

问题


I'm using Paperclip-FFMEG to upload video files to my development environment (and, eventually, to a local server when my project goes into production).

When videos are uploaded, the filename is, by default, as follows:

/system/modelnames/paperclipnames/.../mynewfile.mp4?xxxxxxxxxx

I believe the 10 digit figure after the questionmark is a timestamp.

However, the player I will be using to play the videos doesn't like to have anything after the file attachment - so I would like to strip the questionmark, and the timestamp after it, before passing the URL into the player.

I tried to use the following Ruby (I think) strip function:

temp_variable = model.paperclipattribute.url(:blah).strip('?')[0]

However, Rails throws up an error:

wrong number of arguments(1 for 0)

I take it I'm doing this wrong? Any other solutions? I don't want to switch off timestamps entirely, as I only need to do so in this situation.

Thanks!


回答1:


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



回答2:


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.




回答3:


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

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




回答4:


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



来源:https://stackoverflow.com/questions/11802230/remove-question-mark-from-paperclip-generated-files-in-ruby-on-rails-3-2-6

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