I'm working on a simple api using ruby on rails, and tried to attach an image to my post but I always get this error:
"[paperclip] Link failed with File exists # syserr_fail2_in -
C:/Users/Safouene/AppData/Local/Temp/d5e01d9478f9774f9f669fd29da0cb2720190401-5260-1tjlj3v.png;
copying link
C:/Users/Safouene/AppData/Local/Temp/d5e01d9478f9774f9f669fd29da0cb2720190401-5260-ooex8h.png
to
C:/Users/Safouene/AppData/Local/Temp/d5e01d9478f9774f9f669fd29da0cb2720190401-5260-1tjlj3v.png
Command :: file -b --mime
"C:/Users/Safouene/AppData/Local/Temp/d5e01d9478f9774f9f669fd29da0cb2720190401-5260-1tjlj3v.png""
Completed 204 No Content in 65ms (ActiveRecord: 1.0ms)
Here is My Model :
class Action < ApplicationRecord
belongs_to :user
validates :title ,presence: true
validates :desc ,presence: true
validates :location , presence: true
has_attached_file :picture
validates_attachment :picture, presence: true
do_not_validate_attachment_file_type :picture
end
This is not strictly answering your question but I feel I should point out that since the release of active storage with rails 5.2 many of these file upload gems have been deprecated. This means they will no longer be maintained. (see github page https://github.com/thoughtbot/paperclip)
With that in mind I would strongly advise you to use active storage, rather than paperclip.
Active storage is part of the rails core framework now so you can rest assured it will be maintained and always work the the latest releases of rails.
Plus, active storage is super easy to implement and full of really cool features.
You can read more about active storage here: https://edgeguides.rubyonrails.org/active_storage_overview.html
Related
I'm using Rails 5 with rails_admin and carrierwave gems.
I have a model Photo and and image uploader mounted on it (as per carrierwave documentation), looks roughly like this:
class Photo < ActiveRecord::Base
mount_uploader :image, ImageUploader
belongs_to :project
validates :name, presence: true
validates :image, presence: true
end
Given I have already some Photo objects created I can see a list of them in the rails_admin admin view.
And I start editing one of them
And I edit name
And I proceed to save it
Then rails admin fires some of it's magic and photo is being saved but after this action the image dissapears.
I have been digging a little in what request are being fired and rails_admin fires a PUT request with such params:
{
"authenticity_token"=>"xxx",
"photo"=>{
"name"=>"test2",
"description"=>"ewdeeweeefxxxwefwe",
"project_id"=>"3",
"image_cache"=>"",
"main"=>"0",
"about_us"=>"0"
},
"return_to"=>"http://localhost:3000/panel-admin/photo?model_name=photo", "_save"=>"", "model_name"=>"photo", "id"=>"29"}
and my Photo object is being update with not only name but also with image_url that of course overrides Image that already was mounted to the Photo
I have no idea why this is happening and how to prevent it.
Anyone might have encountered this issue and knows how to resolve it?
I found that. When I uncomment my custom filename method in uploader, it works well.
I'm implementing Carrierwave with fog storage into my Rails App. The whole purpose of this app is to store pdf articles and have the ability for anyone with access to the app to retrieve them. Right now, I have the functionality of storing the article pdf working great. But, now I need to be able to retrieve the pdf from S3. Is this possible? I noticed in the docs there is a uploader.retrieve_from_store!("my_file.png") method. I tried to run this in the console and I got this error NoMethodError: undefined method retrieve_from_store! for ArticleUploader:Class Any help with this would be great! I'm just not finding any suitable answers so far. Thanks!
Article Uploader
class ArticleUploader < CarrierWave::Uploader::Base
storage :fog
def extension_whitelist
%w(jpg jpeg gif png pdf)
end
end
Article Model
class Article < ApplicationRecord
mount_uploader :file, ArticleUploader
validates :title, presence: :true
validates :publication_date, presence: :true
validates :source, presence: :true
end
You can access it with the methods that carrierwave has, in your case:
article = Article.find(1)
article.file.url
If you are on development it will output the path for the file and if you are on production and using S3, it will output the whole url, http://s3.amazonaws.com/<vendor>/articles/1/file.pdf for example.
You can find more information on the official docs:
https://github.com/carrierwaveuploader/carrierwave#activerecord
There is also an old rails cast on that http://railscasts.com/episodes/253-carrierwave-file-uploads
I'm attempting to install CarrierWave gem on ActiveAdmin in rails, and the setup seemed easy enough. However, when I attempt to upload a test image to the /public/uploads directory, the image isn't saved. What's more irritating is the fact that there is no exception being raised, so I don't know where to look in order to find the issue. I can create a post, browse for an image, select that image, and submit the post in order to be saved, but I still end up with IMAGE: EMPTY on the show page in ActiveAdmin as shown below. In the image, I wrote a lorem ipsum post that included an image, and I saved it.
How to I actually get the uploader to upload?
ruby 1.9.3p547 (2014-05-14 revision 45962) [x86_64-darwin10.8.0]
Rails 4.1.6
This is the show page for a single Post object within ActiveAdmin
This is a full page screenshot of the form in question
The same form, but zoomed in. Obviously, I wasn't trying to upload an image in this screenshot.
/app/uploaders/image_uploader.rb
class ImageUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"public/uploads"
end
end
/app/models/post.rb
class Post < ActiveRecord::Base
belongs_to :category
scope :rails, -> { where(category_id: 1) }
extend FriendlyId
friendly_id :title, use: [:slugged, :finders]
mount_uploader :image, ImageUploader
end
After a couple more hours of searching, I found the inspiration for an answer in another question, which you can find here.
The reason I wasn't getting a result at all was because I had not permitted the application to accept an :image in the app/admin/post.rb file. Now, I still don't know why there was no error raised (or even a line logged to the console), but fixing the problem of getting an upload is as simple as permitting the upload parameter as follows:
permit_params :title, :slug, :blurb, :content, :category_id, :image
When I posted this question, I had not added in that last :image, so my application was simply throwing away that parameter instead of saving it within the file system with the rest of the post.
I have a pretty simple model:
class SocialGroup < ActiveRecord::Base
validates :name, presence: true
validates :file, presence: true
mount_uploader :file, SocialGroupFileUploader
end
And the question is: Should I test (with rspec) the model successful save with valid file type provided (my white list of file extensions includes only csv)? Or should I test the file uploader in isolation? If answer on first question is Yes, how test shoul look like?
If your uploader is simple I think it's fairly safe to assume that CarrierWave's developers have done the testing there, the test suite is fairly comprehensive (but it's very much a matter of opinion, some people will and some people won't).
I'd concentrate on making sure that the controller is tested either in rspec or the cucumber specs. There are a couple of examples of people doing this in a google search.
I'm trying to store images associated with a Coupon object in an Amazon S3 instance. My Rails 3.1 application uses Mongoid for document storage, and I'm not attempting to introduce Paperclip (via mongoid-paperclip) to store images for coupons on Amazon S3.
I've created a permission group on Amazon S3 and added a user; the valid permissions have been added to my application (which I can verify, because if I remove or alter the permissions, I receive an error), but when I attempt to save a file, the file's information is stored in the database, but the file is not uploaded. If I remove mongoid-paperclip from the equation, files are not stored locally either (although I do see that they exist in a temp folder on my local machine and are processed via ImageMagick).
Models
My Coupon objects embeds many Image objects as such:
class Coupon
include Mongoid::Document
include Mongoid::Timestamps
# Relationships
embeds_one :image, as: :imageable
# Database Schema
field :name
field :description
field :expires, type: Date
# Validation
validates :name, :description, :presence => true
end
class Image
include Mongoid::Document
include Mongoid::Paperclip
include Mongoid::Timestamps
# Relationships
embedded_in :imageable, polymorphic: true
has_mongoid_attached_file :file,
:path => ':id/:style.:extension',
:storage => :s3,
:s3_credentials => File.join(Rails.root, 'config', 's3.yml'),
:styles => {
:original => ['920x920>', :jpg]
}
end
I do not see any output from Paperclip in my console or logs and cannot determine how to enable such output. The only information logged in relation to the file being uploaded is as follows, immediately before the page is redirected after successfully updating attributes:
| Command :: identify -format %wx%h '/var/folders/ff/vxzlz741287dsr006bv2s59c0000gn/T/stream20111022-80997-o1pqk.png[0]'
| Command :: convert '/var/folders/ff/vxzlz741287dsr006bv2s59c0000gn/T/stream20111022-80997-o1pqk.png[0]' -resize "920x920>" '/var/folders/ff/vxzlz741287dsr006bv2s59c0000gn/T/stream20111022-80997-o1pqk20111022-80997-5z9phe.jpg'
This seems to be a problem with the identify command provided by your local ImageMagick install. Do you have ImageMagick's libraries installed on your system? I had a similar problem once and it seems that installing ImageMagick from brew fixed it up. FYI: My problem was caused by bad symbolic links, the identify command (and others) simply weren't linked correctly from when I compiled ImageMagick from source.
Maybe, This code is not using callbacks.
Paperclip use the callback of after_save to save a image.
embeds_one :image, as: :imageable, cascade_callbacks: true
You should use cascade callbacks of Mongoid.
http://mongoid.org/en/mongoid/docs/callbacks.html