Why is Paperclip failing silently in production + development? - ruby-on-rails

I'm clueless as to what's going on with this because I have another app on the same server that receives and saves uploads just fine.
No error messages, Paperclip even say's in the log it's saving the attachments.
But the attachments don't get saved.
Thoughts anyone?
Also how would I test for this using RSpec/Capybara, because apparently my tests don't cover this.
Running Paperclip 2.3.12, Rails 3.0.9, REE1.8.7
Production on RHEL5 / Apache , but runs on a different user than my other app's user.
Update I get the same silent fail on development too!
But my test's pass and I can see the image being uploaded with the tests.

I believe you forget to add multipart option to your form
:html => {:multipart => true}
so
<%= form_for #my_object, :html => {:multipart => true} do |f| -%>
...
<% end %>

Related

Video shows black screen after uploading with PaperClip AV

I am using PaperClip AV Transcoder, and when I upload a video all I see is a black image. I presume that video is not getting uploaded.
Do I also have to install a video player as well with it, or does that come with the PaperClip AV?
My model that has the video:
class Pin < ActiveRecord::Base
belongs_to :user
has_attached_file :video, :styles => {
:medium => { :geometry => "640x480", :format => 'mp4' },
:thumb => { :geometry => "100x100#", :format => 'jpg', :time => 10 }
}, :processors => [:transcoder]
validates_attachment_content_type :video, :content_type => ["video/mp4", "video.mov", "video/mpeg","video/mpeg4", "image/jpg", "image/jpeg"]
end
My show view for the video:
<%= video_tag #pin.video.url %>
<p>
<strong>Description:</strong>
<%= #pin.description %>
</p>
<% if #pin.user == current_user %>
<%= link_to 'Edit', edit_pin_path(#pin) %>
<% end %>
<%= link_to 'Back', pins_path %>
I don't know what other code would help. I'd gladly write any other lines of code that might help.
A picture of what I am talking about:
This is a screenshot of my ffmpeg version from my command line:
As you are using transcoder as your processor. You need to install this paperclip-av-transcoder gem too. (If already not there, add the to your Gemfile and do: bundle install).
Also, you may need to install ffmpeg in your machine to get the processor to work locally.
After these steps, it should work for you.
There are several steps to debug this.
Firstly, you need to check if the video file has been saved.
If the video file exists, it means your problem lies with playback (not processing). Thus you'll need to browse to public/system/.....
If you're on your dev machine, you can do this by simply browsing to /public/system, if you're using Heroku or another web server, you'll have to SSH into the bash and cd to the directory:
Heroku
$ heroku run bash
$ cd public/system
$ ls
SSH
SSH into your system
cd /app_root/public/system
ls
You'll then be able to browse through the folders to get to the appropriate directory containing your videos...
--
If you have the video on your system, it means they've been processed.
If they're not on your system, you need to consult your logs (post them on here), and show us what the error might have been.
To playback your video, you'll be able to use the HTML5 <video> tag (Rails video_tag):
<%= video_tag #attachment.paperclip_object.url %>
This will show the video (if it's been transcoded into MP4 or another compatible file format), allowing the playback to occur.
Cover
Another issue you will have is the cover image.
It seems you've set this up properly; you can use it as follows:
<%= video_tag #attachment.paperclip_object.url, poster: #attachment.paperclip_object.thumb.url %>

Rails image_tag auto prefixes "/images" before #user.avatar.url instead of "/assets" in paperclip

I am using paperclip to upload user avatar.
Here is User model:
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "user.png", :path => "app/assets/images/:class/:attachment/:id/:basename_:style.:extension", :url => ":class/:attachment/:id/:basename_:style.:extension"
So image is saving in app/assets/images/user/avatar/:id/:basename_:style.:extension
But when I do
<%= image_tag #user.avatar.url %>
It shown as:
<img src="/images/users/avatars/15/99e88dc27c19d8c6163d9cd305f738be_original.jpg" alt="99e88dc27c19d8c6163d9cd305f738be original">
i.e. inserts "/images" instead of "/assets"
I double-checked, the avatar image exists in the assets/images/user/avatar/ folder
Although all other images in page are showing correctly using assets pipeline "/assets/logo-thebighashgohere.png"
NOTE: This works correctly if I manually insert image url as string
i.e.:
<%= image_tag "users/avatars/15/99e88dc27c19d8c6163d9cd305f738be_original.jpg" %>
It correctly shows as
<img src="/assets/users/avatars/15/99e88dc27c19d8c6163d9cd305f738be_original-thebighashgohere.jpg" alt="99e88dc27c19d8c6163d9cd305f738be original thebighashgohere">
I strongly recommend you to not save user-generated-content into the assets folder!
if your website is going into production mode, the assets are compiled and everything you throw in at runtime won't be catched.
stuff like that belongs into the /public directory (!)
to solve your problem
:url => ":class/:attachment/:id/:basename_:style.:extension
you tell paperclip how to generate your "url". with "path" you define where the files are stored internally, with url you control how to generate the routes. your rote is wrong, no asstes path in it.
but again - do not save those picutres into assets !
btw: i wonder if your solution is even possible in productionmode. the assetspipeline is generting files with a digest appending on it, while paperclip knows nothing about those digest it will always render a route without the digest-stamp. by that you can't call the images from the assets pipeline. so your whole concept won't work in production, but i might be wrong

Spree deface override tutorial

I am following the Spree deface overrides developer guide: http://guides.spreecommerce.com/developer/deface_overrides_tutorial.html
My code matches their's exactly, but I keep getting this error. I looked all around but I didn't see anyone else having this problem or anything similar at all:
undefined method `content_tag' for Spree:Module
I am running Rails 4.0.2 and ruby 1.9.3 (it's possible that the tutorial wasn't updated for rails 4?)
here's my code:
app/overrides/add_sale_price_to_product_edit.rb
Deface::Override.new(:virtual_path => 'spree/admin/products/_form',
:name => 'add_sale_price_to_product_edit',
:insert_after => "erb[loud]:contains('text_field :price')",
:text => "
<%= f.field_container :sale_price do %>
<%= f.label :sale_price, raw(Spree.t(:sale_price)) %><span>*</span>
<%= f.text_field :sale_price, :value =>
number_to_currency(#product.sale_price, :unit => '') %>
<%= f.error_message_on :sale_price %>
<% end %>
")
app/models/spree/product_decorator.rb
module Spree
Product.class_eval do
delegate_belongs_to :master, :sale_price
end
end
You're getting the error because the translation for Spree.t(:sale_price) is not specified. This is failing because Rails 4.0.2 made some changes to the I18n API . You have a few choices.
Add a translation for the missing tag and remember that this content_tag issue is caused by this crazy bug.
Downgrade to Rails 4.0.0 (not recommended)
Upgrade spree to the 2-1-stable branch (or wait until 2.1.4 is released)
Apply this change to your local Spree installation. It should correct this issue.
Any of those should get you working again.

Why Ajax renders 2 views in rails 3.1 app

When entering log in our rails 3.1 app, ajax is used to render the input screen below. Here is the link for log:
<%= link_to 'Log', new_part_out_log_path(#part, :format => :js), :remote => true, :id => 'new_log_link' %>
And the new.js.erb as this:
$("<%= escape_javascript render(:file => 'out_logs/new.html.erb') %>").insertAfter('#new_log_link');
$('#new_log_link').hide();
$('#close').hide();
the problem is that after clicking 'Log', instead of one view, 2 identical views of out_logs/new.html.erb were rendered. What may be wrong with our code? thank so much.
The problem is related to upgrade to ruby 1.9.3 which causes webrick server (for development) warning message and also rendering the .js.erb file twice on screen. The twice rendering problem disappears on our production server which is running nginx. The following link may help to understand the problem:
http://theresa.multimediatechnology.at/webrick-warnings-in-ruby-1-9-3/
What does "WARN Could not determine content-length of response body." mean and how to I get rid of it?

Rails upload file to ftp server

I'm on Rails 2.3.5 and Ruby 1.8.6 and trying to figure out how to let a user upload a file to a FTP server on a different machine via my Rails app. Also my Rails app will be hosted on Heroku which doesn't facilitate the writing of files to the local filesystem.
index.html.erb
<% form_tag '/ftp/upload', :method => :post, :multipart => true do %>
<label for="file">File to Upload</label> <%= file_field_tag "file" %>
<%= submit_tag 'Upload' %>
<% end %>
ftp_controller.rb
require 'net/ftp'
class FtpController < ApplicationController
def upload
file = params[:file]
ftp = Net::FTP.new('remote-ftp-server')
ftp.login(user = "***", passwd = "***")
ftp.putbinaryfile(file.read, File.basename(file.original_filename))
ftp.quit()
end
def index
end
end
Currently I'm just trying to get the Rails app to work on my Windows laptop. With the above code, I'm getting this error
Errno::ENOENT in FtpController#upload
No such file or directory -.... followed by a dump of the file contents
I'm trying to upload a CSV file if that makes any difference. Anyone knows what's going on?
After much research and head banging, I ended up reading the source code for putbinaryfile method to figure out a workaround for the limitation of putbinaryfile. Here's the working code, replace this line
ftp.putbinaryfile(file.read, File.basename(file.original_filename))
with
ftp.storbinary("STOR " + file.original_filename, StringIO.new(file.read), Net::FTP::DEFAULT_BLOCKSIZE)
And in case you are wondering, STOR is a raw FTP command, yeah it came to that. I'm rather surprised this scenario isn't more easily handled by Ruby standard libraries, it certainly wasn't obvious what needed to be done.
And if your app is on Heroku, add this line
ftp.passive = true
Heroku's firewall setup does not allow for FTP active mode, also make sure that your FTP server supports passive mode.
Seems to me that ftp.putbinaryfile just wants the path and name of the file as the first parameter.

Resources