Rails: 渲染回形针上传的视频

Rails: Rendering video uploaded by paperclip

我正在使用 Rails 6.1 + Ruby 3 并尝试允许用户上传视频,然后在应用程序中观看。

clip.rb

class Clip < ApplicationRecord
    belongs_to :chapter
    has_attached_file :video, styles: {
        :medium => {
          :geometry => "640x480",
          :format => 'mp4'
        },
        :thumb => { :geometry => "160x120", :format => 'jpeg', :time => 10}
      }, :processors => [:transcoder]
    validates_attachment_content_type :video, content_type: /\Avideo\/.*\Z/ 
end

show.html.erb

<video autobuffer="autobuffer" autoplay="autoplay" id="hero-video" loop="loop" src="<%=  @clip.video %>"></video>

我已经在 RailsAdmin 中成功上传了这个视频,但是当我尝试查看它时,出现了这个错误:

undefined method `escape' for URI:Module

这是@clip.video 带回来的:

=> #<Paperclip::Attachment:0x00007feaaa84f728 @name=:video, @instance=#<Clip id: 5, order: 1, tts: "First up, skating. Need skates? Get fitted for fre...", tts_url: nil, created_at: "2021-08-27 18:20:35.181752000 +0000", updated_at: "2021-08-27 18:48:53.832933000 +0000", chapter_id: 1, video_file_name: "test.mp4", video_file_size: 6707510, video_content_type: "video/mp4", video_updated_at: "2021-08-27 18:48:46.276117000 +0000">, @options={:convert_options=>{}, :default_style=>:original, :default_url=>"/:attachment/:style/missing.png", :escape_url=>true, :restricted_characters=>/[&$+,\/:;=?@<>\[\]\{\}\|\\^~%# ]/, :filename_cleaner=>nil, :hash_data=>":class/:attachment/:id/:style/:updated_at", :hash_digest=>"SHA1", :interpolator=>Paperclip::Interpolations, :only_process=>[], :path=>":rails_root/public:url", :preserve_files=>false, :processors=>[:transcoder], :source_file_options=>{}, :storage=>:filesystem, :styles=>{:medium=>{:geometry=>"640x480", :format=>"mp4"}, :thumb=>{:geometry=>"160x120", :format=>"jpeg", :time=>10}}, :url=>"/system/:class/:attachment/:id_partition/:style/:filename", :url_generator=>Paperclip::UrlGenerator, :use_default_time_zone=>true, :use_timestamp=>true, :whiny=>true, :validate_media_type=>true, :check_validity_before_processing=>true}, @post_processing=true, @queued_for_delete=[], @queued_for_write={}, @errors={}, @dirty=false, @interpolator=Paperclip::Interpolations, @url_generator=#<Paperclip::UrlGenerator:0x00007feaaa84f638 @attachment=#<Paperclip::Attachment:0x00007feaaa84f728 ...>, @attachment_options={:convert_options=>{}, :default_style=>:original, :default_url=>"/:attachment/:style/missing.png", :escape_url=>true, :restricted_characters=>/[&$+,\/:;=?@<>\[\]\{\}\|\\^~%# ]/, :filename_cleaner=>nil, :hash_data=>":class/:attachment/:id/:style/:updated_at", :hash_digest=>"SHA1", :interpolator=>Paperclip::Interpolations, :only_process=>[], :path=>":rails_root/public:url", :preserve_files=>false, :processors=>[:transcoder], :source_file_options=>{}, :storage=>:filesystem, :styles=>{:medium=>{:geometry=>"640x480", :format=>"mp4"}, :thumb=>{:geometry=>"160x120", :format=>"jpeg", :time=>10}}, :url=>"/system/:class/:attachment/:id_partition/:style/:filename", :url_generator=>Paperclip::UrlGenerator, :use_default_time_zone=>true, :use_timestamp=>true, :whiny=>true, :validate_media_type=>true, :check_validity_before_processing=>true}>, @source_file_options={}, @whiny=true, @normalized_styles={:medium=>#<Paperclip::Style:0x00007feaaa84c988 @name=:medium, @attachment=#<Paperclip::Attachment:0x00007feaaa84f728 ...>, @geometry="640x480", @format="mp4", @processors=nil, @convert_options=nil, @source_file_options=nil, @other_args={}>, :thumb=>#<Paperclip::Style:0x00007feaaa84c910 @name=:thumb, @attachment=#<Paperclip::Attachment:0x00007feaaa84f728 ...>, @geometry="160x120", @format="jpeg", @processors=nil, @convert_options=nil, @source_file_options=nil, @other_args={:time=>10}>}>

虽然我不是 100% 确定这是问题所在,但我认为 Paperclip 库已被弃用,它不适用于 ruby 3。如果您使用最新的 ruby 2 版本应该适合你。但是,我建议新项目不要依赖回形针。