ruby-on-railspaperclip

Rails: Rendering video uploaded by paperclip


I am using Rails 6.1 + Ruby 3 and trying to allow users to upload videos and then watch them in app.

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>

I've been uploading this video successfully in RailsAdmin, but when I try to view it, I get this error:

undefined method `escape' for URI:Module

Here is what @clip.video brings back:

=> #<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}>}>

Solution

  • while I'm not 100% sure this is the problem, I believe the Paperclip library is deprecated and it does not work with ruby 3. If you use the latest ruby 2 version is should work for you. However, I would recommend not depending on paperclip for new projects.