Rendering Paperclip URL's using JBUILDER - ruby-on-rails

I am building an API with Ruby on Rails.
I have Users that have avatars implemented using paperclip.
I am trying to access my paperclip URL's in my JSON output and it just crashed my heroku instance. This works perfectly locally.
Snippet of My User Model
class User < ActiveRecord::Base
has_attached_file :avatar, styles: { large: "600x600#", medium: "300x300#", thumb: "100x100#" }, default_url: "https://s3.amazonaws.com/whiztutor-marketing/photos/Profile300.png"
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
end
Snippet of My Jbuilder for my user
json.cache! #user do
json.id #user.id
json.type #user.type
json.f_name #user.f_name
json.about #user.about
json.email #user.email
json.avatar_url #user.avatar.url(:medium)
json.referral_code #user.referral_code
json.education_level #user.education_level
json.photo_url #user.avatar.to_json
json.education_details #user.education_details.all
json.all_subjects #user.subjects.all
json.all_times #user.all_available_times.each do |time|
json.day time.schedule.to_s
json.time_block time.time_as_string
json.next_occurrence time.schedule.next_occurrence(Time.now)
end
end
I tried wrapping this into a method as found on this question and it breaks the server the exact same way. I can even run console and access these URL's directly with the server. Something with JBUILDER and PAPERCLIP just don't mix and I can't seem to get to the bottom of it. Any help is greatly appreciated.

Finally diagnosed this issue after several hours of research.
The issue is with the "json.cache! #user do" - Specifically the .cache! - I wanted to save a few server cycles and speed things up so this is how I implemented things at first.
Regardless, when I updated my JBUILDER code to the following I am no longer getting 500 server errors.
Snippet of My working Jbuilder for my user
json.id #user.id
json.type #user.type
json.f_name #user.f_name
json.about #user.about
json.email #user.email
json.small_photo #user.profile_url_small
json.medium_photo #user.profile_url_medium
json.referral_code #user.referral_code
json.education_level #user.education_level
json.education_details #user.education_details.all
json.all_subjects #user.subjects.all
json.all_times #user.all_available_times.each do |time|
json.day time.schedule.to_s
json.time_block time.time_as_string
json.next_occurrence time.schedule.next_occurrence(Time.now)
end

It's hard to say what the problem might be without knowing the error, but these things look suspicious or just wrong:
json.avatar_url #user.avatar.url(:medium)
json.photo_url #user.avatar.to_json
The second one is going to give you extra quotes you probably don't want. And why have both?
Also here:
json.all_times #user.all_available_times.each do |time|
json.day time.schedule.to_s
json.time_block time.time_as_string
json.next_occurrence time.schedule.next_occurrence(Time.now)
end
I think you want this:
json.all_times #user.all_available_times do |time|
json.day time.schedule.to_s
json.time_block time.time_as_string
json.next_occurrence time.schedule.next_occurrence(Time.now)
end

You can try this to get the url in json response, it works for me placed this method in your model.
def avatar_url
avatar.url(:medium)
end
And call this method from controller,by overriding to_json()
render :json => #friends.to_json(:methods => [:avatar_url])

Related

bson_dump error when try to attach a file to mongoid paperclip field of a rails model

I have spent a lot of time trying to attach a file into a mongoid paperclip field. The examples that I have found, always do the same:
my_model_instance = MyModel.new
file = File.open(file_path)
my_model_instance.attachment = file
file.close
my_model_instance.save!
as you can see in How to set a file upload programmatically using Paperclip.
The model that I have done is:
class Logotype
include Mongoid::Document
include Mongoid::Paperclip
has_mongoid_attached_file :logo,
styles: {large: ['640x160'], small: ['300x300>']},
size: { in: 0..3.megabytes },
content_type: [ "image/jpg", "image/png", "image/bmp" ],
storage: :filesystem,
path: ':rails_root/public/resource/resources/:id/:style.:extension',
url: '/logos/resources/:id/:style.:extension'
validates_presence_of :logo
validates_uniqueness_of :logo
end
However I have always the same big error:
NoMethodError: undefined method `__bson_dump__' for /logos/resources/51b5bfaa69fd8a7941000005/original.jpg?1370865578:Paperclip::Attachment
...
Could someone help me? Thanks in advance!
PD: Sorry for my English level.
This is an old question but I just thought I'd share my solution in case someone else finds themselves here.
For me, this was happening in the Moped::BSON::Extensions::Hash module while trying to log the request for a save action. Your stack may be different, but basically the problem occurs when trying to dump data that Mongoid doesn't know how to handle. In my case it was a file upload, ActionDispatch::Http::UploadedFile, but for the OP it's a Paperclip::Attachment.
I solved it by just removing those key/value pairs from the request parameters, e.g.
# recursively remove UploadedFile from a Hash of request parameters
def remove_files(params)
p = proc do |_, v|
v.delete_if(&p) if v.respond_to? :delete_if
v.is_a?(ActionDispatch::Http::UploadedFile)
end
params.delete_if(&p)
end
There might be a better solution that logs the filename or something instead, but I found it easier just to get rid of it.

File uploading with Dragonfly, impossible to access images after upload (Rails 3)

I'm trying to make a multiple drag and drop upload file system with Rails 3 and Dragonfly (or anything that would work actually)
I'm at the point where my file comes in my controller through the params hash and I can retrieve it as an ActionDispatch::Http::UploadedFile so I thought all I would have to do then is push it in my model's attribute image but it doesn't work
This my Picture model :
class Picture < ActiveRecord::Base
image_accessor :image
attr_accessible :image_name, :image_uid, :title
end
I thought this would work in my controller :
def createImage
#new_picture = Picture.new
#new_picture.image = params[:pic]
if #new_picture.save
render :json => { :picture => #new_picture }
end
end
Ok, so this registers the record with image_name nil oddly, but with the image_uid set
However, when I try to access my image <%= image_tag #picture.image.url %> I get a not found error
For example :
Request URL:http://localhost:3000/media/BAhbBlsHOgZmSSIhMjAxMi8wOS8yMi8xOV8zMF8yOF83MzBfZmlsZQY6BkVU
Request Method:GET
Status Code:404 Not Found
I'm using ruby 1.9.3 and rails 3.2.8
Any ideas ? :D
Dragonfly needs to load it's rack adapter before it is able to take encoded url requests (like the one above). The best way to load it is to add this before config.encoding = "utf-8 in your /config/application.rb
config.middleware.insert 0, 'Rack::Cache', {:verbose => true,:metastore => URI.encode("file:#{Rails.root}/tmp/dragonfly/cache/meta"),:entitystore => URI.encode("file:#{Rails.root}/tmp/dragonfly/cache/body")} unless Rails.env.production?
config.middleware.insert_after 'Rack::Cache', 'Dragonfly::Middleware', :images
Please note that you will need the Rack-Cache gem as well.
Hope this helps!

save_and_process post processing 403 Forbidden Carrierwave_direct S3 Fog

I'm trying to develop direct fileuploads to S3 for my app. I'm following the github tutorial for this and everything is more or less ok but get an error message when trying to make the post processing.
I did the following:
I have an activerecord model called clip.rb:
class Clip < ActiveRecord::Base
belongs_to :attachable, :polymorphic => true
mount_uploader :avatar, AvatarUploader
attr_accessible :id, :avatar, :name, :clipat_file_name, :attachable_id, :attachable_type, :clipat, :project_id, :user_id, :path, :parent_id,
def save_and_process_avatar(options = {})
if options[:now] or 1==1
self.remote_avatar_url = avatar.direct_fog_url(:with_path => true)
save
else
Resque.enqueue(AvatarProcessor, attributes)
end
end
Then I have an uploader: avatar_uploader.rb
class AvatarUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
include CarrierWaveDirect::Uploader
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}" #I removed /#{model.id} from the template because it creates an empty directory on the server. But If I put it back, the same problem remains
end
version :thumb do
process :resize_to_limit => [50, 50]
end
end
and an avatar controller:
class AvatarsController < ApplicationController
def new
#uploader = Clip.new.avatar
#uploader.success_action_redirect = 'http://localhost:3000/clips'
end
end
and finally my clip_controller:
class ClipsController < ApplicationController
def index
if params[:key]
key=params[:key].split("/")
clip = Clip.new
clip.attachable_id = key[3]
clip.attachable_type = "Pmdocument"
clip.key = params[:key]
# clip.save
clip.save_and_process_avatar
end
#clips = Clip.where("avatar is not null")
respond_to do |format|
format.html # index.html.erb
format.json { render json: #clips.collect { |p| p.to_jq_upload }.to_json }
end
end
When I upload a file, if I just save my "clip", everything is ok. If I use the save_and_process method however, an error arises at line:
self.remote_avatar_url = avatar.direct_fog_url(:with_path => true)
This is the error message:
OpenURI::HTTPError (403 Forbidden):
app/models/clip.rb:38:in `save_and_process_avatar'
app/controllers/clips_controller.rb:22:in `index'
Rendered /Users/nico/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.4ms)
Rendered /Users/nico/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.2ms)
Rendered /Users/nico/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (5.2ms)
I've been hanging on this for two days so, any help would be greatly appreciated!!! Thanks!!! Nicolas.
My bet is that the URL supplied to self.remote_avatar_url is incorrect. I had this same problem and the code that CWDirect gem provides didn't work for me and gave me an incorrect URL, thus CarrierWave couldn't download and process the image. The whole 403 Forbidden error message was a crap message from Amazon--this leads one to believe that there is something wrong with Permissions; in my case there was absolutely nothing wrong with permissions. It was just that there was no image there. Here is the code that got this working for me, notice I've changed how the URL is formed:
def save_and_process_image(options = {})
if options[:now]
# debugger
self.remote_image_url = image.direct_fog_url+self.key # OLD CODE THAT AINT WORKIN! --> image.direct_fog_url(:with_path => true)
save
else
# Resque.enqueue(AvatarProcessor, attributes)
# TODO: Implement background processing
end
end
Note that the name of my mounted field is image and not avatar.
How I got to this point and fixed it--try this out, use the rails debugger (just uncomment the debugger line above) to freeze the program just before the self.remote_image_url line, then while in debug mode type irb to start up the console. Then you can print out and see really what value 'image.direct_fog_url(:with_path => true)' is giving you. You can copy and paste this into a browser. If it's wrong (probably is) then you will get the silly Permissions error (even though it's not a permissions problem), but when it's correct either you will see the uploaded image or the image will download.
It's a good idea to have your Amazon S3 console open and viewing your dev bucket so you can find the image that just uploaded. Find the image in the console and go to its properties and you can see the web address/url that you are supposed to be using.
Hope this helps. Because of the misleading error this was hard to track down for me, I spent a bunch of time trying to correct permissions on my S3 bucket but this wasn't the problem, just that code from the CWDirect github page doesn't work for me (gem version??).

How can I reference images in the asset pipeline from a model?

I have a model with a method to return a url to a person's avatar that looks like this:
def avatar_url
if self.avatar?
self.avatar.url # This uses paperclip
else
"/images/avatars/none.png"
end
end
I'm in the midst of upgrading to 3.1, so now the hard-coded none image needs be referenced through the asset pipeline. In a controller or view, I would just wrap it in image_path(), but I don't have that option in the model. How can I generate the correct url to the image?
I struggled with getting this right for a while so I thought I'd post the answer here. Whilst the above works for a standard default image (i.e. same one for each paperclip style), if you need multiple default styles you need a different approach.
If you want to have the default url play nice with the asset pipeline and asset sync and want different default images per style then you need to generate the asset path without fingerprints otherwise you'll get lots of AssetNotPrecompiled errors.
Like so:
:default_url => ActionController::Base.helpers.asset_path("/missing/:style.png", :digest => false)
or in your paperclip options:
:default_url => lambda { |a| "#{a.instance.create_default_url}" }
and then an instance method in the model that has the paperclip attachment:
def create_default_url
ActionController::Base.helpers.asset_path("/missing/:style.png", :digest => false)
end
In this case you can still use the interpolation (:style) but will have to turn off the asset fingerprinting/digest.
This all seems to work fine as long as you are syncing assets without the digest as well as those with the digest.
Personally, I don't think you should really be putting this default in a model, since it's a view detail. In your (haml) view:
= image_tag(#image.avatar_url || 'none.png')
Or, create your own helper and use it like so:
= avatar_or_default(#image)
When things like this are hard in rails, it's often a sign that it's not exactly right.
We solved this problem using draper: https://github.com/jcasimir/draper. Draper let us add a wrapper around our models (for use in views) that have access to helpers.
Paperclip has an option to specify default url
has_attached_file :avatar, :default_url => '/images/.../missing_:style.png'
You can use this to serve default image' in case user has not uploaded avatar.
Using rails active storage I solved this problem by doing this:
# Post.rb
def Post < ApplicationRecord
has_one_attached :image
def thumbnail
self.image.attached? ? self.image.variant(resize: "150x150").processed.service_url : 'placeholder.png';
end
def medium
self.image.attached? ? self.image.variant(resize: "300x300").processed.service_url : 'placeholder.png';
end
def large
self.image.attached? ? self.image.variant(resize: "600x600").processed.service_url : 'placeholder.png';
end
end
Then in your views simply call:
<%= image_tag #post.thumbnail %>,

Rails Paperclip, DRY configuration

In order to DRY up my code for attachments pictures, I created an initializer to override the #default_options variable used by Paperclip.
This way, I don't have to specify again and again the url, path and storage I want.
I'd like to go a step further and include the validation in it but I can't make it work...
Any Idea?
EDIT 1: I want at least to validate both presence and size.
EDIT 2: Part of my current code
module Paperclip
class Attachment
def self.default_options
if Rails.env != "production"
#default_options = {
:url => "/assets/:class/:attachment/:id/:style/:normalized_name",
:path => ":rails_root/public/assets/:class/:attachment/:id/:style/:normalized_name",
:default_style => :original,
:storage => :filesystem,
:whiny => Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails]
}
else
...
end
end
end
normalized_name is an outside function, feat: http://blog.wyeworks.com/2009/7/13/paperclip-file-rename
EDIT 3:
This blog: http://omgsean.com/2009/02/overriding-paperclip-defaults-for-your-entire-rails-app/ presnents the default_options hash with a validations key.
So it could be possible, not found yet though.
You will not be able to move the validations into a default_options hash (as these validations are performed outside the attachment class (inside a paperclip module). My thought is that if you have the same validations across all your models, you might need to look into using inheritance to decrease code duplication. I would advise against moving validations into an initializer.

Resources