I am currently using paperclip to upload images to my rails app. This is probably a very simple fix but how or where do I save the missing images to? This is the error that is produced from not having any missing images. How do I change this?
ActionController::RoutingError (No route matches "/photos/normal/missing.png"):
If you don't need any control over the default image, which I don't think you need, you can place it in any folder under RAILS_ROOT/public/images/
Just make sure you point it out in the attachment model with the :default_url parameter. So for example if you place the image in RAILS_ROOT/public/images/normal/missing.png you need t specify the path like this:
has_attached_file :photo, :default_url => "/images/normal/missing.png"
Related
I am trying to get images to upload as flyers for events. I do have the parameters configured in the controller. No error is being shown, but the actual image is not rendering in the view (all you see is the filename).
When I upload an image file in production (hosting with Heroku) it saves properly and I can see the file name and size in the console. I can even go to my Amazon S3 console and look at the actual image from there.
This makes it hard for me to tell what's wrong.
I have a hunch that I'm about to try out (among many other things). The only thing I think it could be is that I haven't configured the url to the location of the file in my S3 bucket, but so far I haven't found out exactly how to do that...
In case you're wondering, I do believe I have the code for the view, controller, and model written properly. But the model does make me curious. Here it is just in case:
event.rb
has_attached_file :flyer,
styles: { medium: '300x430#', thumb: '123x170#' }
validates_attachment_content_type :flyer, :content_type => /\Aimage\/.*\Z/
private
def flyer_size
if flyer.size > 5.megabytes
errors.add(:flyer, 'must be less than 5MB')
end
end
Any ideas on what's going on?
EDIT:
When I inspect element this is what it shows.
<img src= "https://s3.amazonaws.com/[bucketname]/events/flyers/000/000/008/medium/JAM.jpg?1436400492" alt="Jam" >
::before
</img>
Sorry for maybe dumb question, but I have some misunderstanding about how Paperclip works. If I use it with aws-sdk gem, where image urls are stored?
My model has only fields like image_name, image_size, but not something like image_url. I also tried to list all tables from my db in sql console, but there wasn't any special image-related tables. I know what I can access url by calling my_model.image.url, but I need understanding, where this url is located.
Thanks.
The urls are auto generated based on the ruleset you have in your paperclip config file. They usually include the name and id of your model unless you have changed the defaults. Set the :default_url option when you call has_attached_file if you want to change it:
:default_url => '/images/:attachment/missing_:style.png',
:path => "avatars/:id/:style/:filename"
More background: rails paperclip default image with S3
I'm using Paperclip-FFMEG to upload video files to my development environment (and, eventually, to a local server when my project goes into production).
When videos are uploaded, the filename is, by default, as follows:
/system/modelnames/paperclipnames/.../mynewfile.mp4?xxxxxxxxxx
I believe the 10 digit figure after the questionmark is a timestamp.
However, the player I will be using to play the videos doesn't like to have anything after the file attachment - so I would like to strip the questionmark, and the timestamp after it, before passing the URL into the player.
I tried to use the following Ruby (I think) strip function:
temp_variable = model.paperclipattribute.url(:blah).strip('?')[0]
However, Rails throws up an error:
wrong number of arguments(1 for 0)
I take it I'm doing this wrong? Any other solutions? I don't want to switch off timestamps entirely, as I only need to do so in this situation.
Thanks!
If you want to do this everywhere for a given attachment and without the need to pass the extra parameter, you can set the use_timestamp option when calling the has_attached_file method in your model. So, to build on the example given in the Paperclip README:
has_attached_file :avatar,
:styles => { :medium => "300x300>", :thumb => "100x100>" },
:default_url => "/images/:style/missing.png",
:use_timestamp => false
Hope this is OK to put as an answer to my own question (as it may be useful for others who stumble across this post), but I've since discovered that an alternative (and more appropriate) way to deal with this issue is to add the false parameter to URL() as follows when displaying the content in your view:
model.paperclipattribute.url(:whateverstyle, false)
The timestamp will automatically be removed. I think this is better, as the split method I suggested might remove content you don't intend to remove - for example, if your file is called something like "Is_this_a_question_?_Yes_it_is.mp4?xxxxxx", then everything after the first question mark might be removed (i.e. the file will be read as "Is this a question_", thus corrupting the filename.
I haven't tested this, so I may be wrong.
Globally default them to off, just put this in a config/initializers/paperclip.rb file.
Paperclip::Attachment.default_options[:use_timestamp] = false
You want to use split instead I believe.
strip doesn't take any argument, it just removes leading and trailing whitespaces
I've had a system thats been running fine for ages using Rails 3 & Paperclip 2.3.6 for members images.
class Image < ActiveRecord::Base
belongs_to :business
has_attached_file :data, :styles => {:normal => ["665x443#", :jpg],:thumb => ["104x50#", :jpg]}, :convert_options => {:all => "-channel RGB -strip -quality 80"}
end
Now for some reason the route for the images has changed to /system/data/ instead of previous /system/datas/
I cant figure out whats happened. All of the images still reside in /system/datas/ and a member uploaded some new images and Paperclip has put them in /system/data/
Anyone have any ideas whats happened? I cant figure it out. Thanks.
EDIT: Ok, I had a test box still on Rails 3.0.3. My Release version was on 3.0.7. If I roll back to Rails 3.0.3 it's working again - the path /system/data/ becomes /system/datas/ again.
Rails 3.0.5 introduced some additional inflections which meant 'datas' was no longer used as the plural of 'data', which broke the directory naming for Paperclip!
According to the paperclip documentation you can specify a path for saving images in your model using the :path option and providing a path.
You can find the documentation here.
I was wondering there was a way to specify a different image for the one to be processed by paperclip?
So instead of the user uploading an image, an image url would be used instead or an exsiting different image on your server thought could just be pointed to?
Cheers.
Edit:
Just to be clear, what I'm looking for is, when an image is uploaded, processed, moved to a folder and attached to a folder. That base image is not uploaded via a form but instead is fetched from a URL and them processed, moved etcÂ
Here's a method to use a RemoteFile instead of an uploaded file... Check out the blog post for how to create a RemoteFile, which is a subclass of TempFile
#console
remote_file = RemoteFile.new("http://www.google.com/intl/en_ALL/images/logo.gif")
remote_file.original_filename #=> logo.gif
remote_file.content_type #= image/gif
#controller
def import
#...snip
#imported_user.images.create(:file => RemoteFile.new( url_to_image ))
#...snip
end
http://coryodaniel.com/index.php/2010/03/05/attaching-local-or-remote-files-to-paperclip-and-milton-models-in-rails-mocking-content_type-and-original_filename-in-a-tempfile/
I'm not sure if your requirement is for the user to provide this image or not, but there is a way to have a default image if they don't provide one. This is done using the default_url and default style options like so:
has_attached_image :my_image
:default_url => "/path/to/default_image.jpg",
:default_style => :thumb