I am doing the One Month Rails Course. I am using Rails 4. My Github folder is: https://github.com/phanatuan/pinteresting
I use the Paperclip gem. In the App/views/pins/show.html.erb, I want to show the pins picture at medium size. I passed the code <%= image_tag #pin.image.url %>. The browser shows the pin with the un-resized pictures. When i pass ":medium" into the code as such <%= image_tag #pin.image.url(:medium) %>, the browser shows the missing image.
I check all the documentations and follow closely the instructions but I could not find the mistake.
Really Appreciate your help
Tuan
Its hard to diagnose this without seeing the code, but my best guess is that you did not declare the style :medium for the attached file in your model.
Try this:
has_attached_file :image,
:styles => { :medium => "120x120>" }
Also, make sure that you specify a :style in your path declaration for the file.
Related
I have been stuck on a problem for three weeks now, and although I have tried everyday to find an answer to my problem, I haven't found anything that I have been able to make work. If anyone could help me, I would be very, very grateful.
I have built a basic blog in Rails that allows an admin user to publish articles using a form, containing a title text_field, body text_area, and file_field for an image, which uses Paperclip. All very simple and easy and no problems there. However, I have been trying to alter the form to allow an admin user to upload multiple images as part of a new article, but have been unable to find a way to do this.
I need a solution that will allow me to achieve multiple image upload functionality in a form. I have tried many gems and many tutorials, but have been unable to integrate anything into my app without completely breaking it. Can any one help me get out of this dead end I've found myself in for the last three weeks? Do you have any clear tutorials or solutions that will help me overcome this problem?
Thank you very much to anyone that can help me.
Your question is quite broad, without any schema or anything so I'll try to help you as much as possible given that.
If it's a blog, you might have a post model.
You could have a photo model, with a reference to the post model. If you add paperclip to your photo model, then you can save multiple photos.
There are multiple ways to do this. The first one I think of is using nested forms with a gem like cocoon.
The second I think of is using a dropzone, which would upload the photos using Ajax.
By default PaperClip allows you to upload one attachment. However, a clean way I've found to work around this is to create another model which would wrap around the images.
class Article < AR::Base
has_many :images
accepts_nested_attributes_for :images
end
class Image < AR::Base
belongs_to :article
has_attached_file :filename, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :filename, content_type: /\Aimage\/.*\Z/
end
Don't forget to run a migration to create the images table:
rails generate model image article:references
and
rails generate paperclip image filename
Then in your articles/form, where you initially had the field to upload the image, you would have
<%= f.fields_for :images do |p| %>
<%= p.label :filename %><br>
<%= p.file_field :filename%>
<% end %>
In your articles_controller#article_params, along with the current params, you would have, params.require(:article).permit(..., images_attributes: [:filename])
Afterwards, allowing the uploads of multiple images would therefore only require the basic knowledge of nested forms.
To set a default number of file_fields, in your ArticlesController#new after initializing your #article you would have
def new
#article = Article.new
2.times{ #article.images.build}
end
With this setup, when you navigate to your new_articles route, you should see your article ready to upload 2 images.
To learn more about how to use NestedForms to do even more, you should see Ryan Bates tutorial.
This approach provides a cleaner interface, as the other approaches, I guess, would require some sort of hack.
Let me know if I'm able to help or further clarifications required.
What is the point of using the :url option with paperclip? The :path option does in fact change the location where the file is saved, but the :url option doesn't seem to do a thing. It only works when it points to a publicly accessible file location. At that point, the url is already accessible to anyone. If I change the url so that it doesn't match the path, it does not work. As far as I can tell, it does not create any routes either way. Is there something I am missing here. What is the point of this option? It seems overly confusing to let someone specify a :url without actually creating a route.
I found this post useful in understanding the difference between :path and :url.
:path sets the directory in your application where the file is stored.
:url sets the url that users can use to access the image.
You are right, paperclip does not create a route for you. However, the :url option does give you the ability to select which (existing) route your users can use to download a specific image.
:path and :url usually go hand in hand. If you stick to the paperclip :default_url the path is already configured for you. Just hit the url and everything will work fine.
Changing the file location
In this example I am rendering a users avatar:
<%= image_tag #user.avatar.url %>
Now, lets say that you wanted to change the location that images are stored, you can add the following code to your model:
has_attached_file :avatar,
:path => "public/system/:class/:id/:filename"
However, the image will not render successfully. This is because the new path, where your images are stored, does not match the :default_url. Therefore, you will also need to specify a new url:
has_attached_file :avatar,
:path => "public/system/:class/:id/:filename"
:url => "/system/:class/:id/:basename.:extension"
Now the image url matches the location that the file is stored on your server and the image renders successfully.
Path vs URL
To summarize, :url tells paperclip where abouts on the server to look for an image. :path tells paperclip where to upload an image, when creating or updating a record.
Both :path and :url should point to the same location, in order to render an image successfully.
I use paperclip to upload a users avatar. The image is stored correctly in the /public directory. However I cant figure out how I can get the image displayed. I played with the :url and :path settings for about an hour and cant match them in a way the image will be displayed in the browser.
There is always a 'images/localhost' in the GET-requests path that I can not get rid of.
Here is my code:
user.rb
class User < ActiveRecord::Base
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "missing.png",
:url => ':class/:id/:style.:extension',
:path => ':url'
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
end
The path in the html-tag looked up by the request looks like this:
<img alt="Original" src="/images/localhost/users/1/original.png?1405249849" />
The correct request which returns the image would be
http://localhost:3000/users/1/original.png?1405248482.
How can I get the request match the correct file-system-path. Or: what is wrong with my settings?
I didnt change the application.rb or the development.rb
Best regards,
Kev
you should take url or path helpers of paperclip wherever possible. So to show the image use:
<%= image_tag #user.avatar.url(:medium) %>
Then:
the image url isn't a file system path. Depending on the storage you use in paperclip, images can reside in different places, see understanding storage of the paperclip gem.
If you use file storage, the files are store somewhere like
public/system/users/avatar/000/000/013/small/my_pic.png
Timestamp
What you're seeing is Paperclip's timestamp - the last time the file / object was updated.
Although I can't find any official reference to this number, it basically allows you to determine which files you're dealing with. According to the question referenced above, it's apparently there for if you want to ensure your visitors see the latest version of the file (IE never gets stored in the cache)
I'm not sure why there is a disparity between your stored image & your path. However, I would say the path is correct; you just need to be able to
--
Bottom line - if your image shows on the page, I don't think there's any systemic issue with your path; if it doesn't show on the page, can you provide logs / reference to the error?
This post, https://stackoverflow.com/a/26222093/1949363, which pointed to http://www.bwigg.com/2009/10/paperclip-customizing-paths-and-urls/ was the answer for me. I was having issues only in my test environment but I believe the fix should work in other environments as well.
Try the following settings:
:path => "public/system/:class/:id/:filename",
:url => "/system/:class/:id/:basename.:extension"
This question already has answers here:
How do you access the raw content of a file uploaded with Paperclip / Ruby on Rails?
(7 answers)
Closed 5 years ago.
I have paperclip running to upload and store files in a non-public directory on the server. Now I want to be able to read the files directly and or feed them into a gem such as axlsx. I'm struggling with even simply looping threw a text file and think I'm missing something basic (as is usually the case).
Here is my first attempt at opening the file:
Paperclip config in application.rb:
config.paperclip_defaults = {:storage => :fog, :fog_credentials => {:provider => "Local", :local_root => "#{Rails.root}/secured_storage"}, :fog_directory => "", :fog_host => "localhost"}
Model:
class Census < ActiveRecord::Base
has_attached_file :censusfile
validates_attachment_content_type :censusfile,
:content_type => ["application/octet-stream", "text/plain"]
end
In the Controller:
def processcensus
#census=Census.find(params[:id])
#file=#census.censusfile.path
end
In the View:
<% File.readlines(#file).read do |line| %>
<%= line %>
<% end %>
This fails because the 'path' returned by Paperclip is the path relative to its sotrage path, not the full path.
UPDATE: If I add the directory (in this case "secured_storage" in from of the path it work as expected. For example:
#file="secured_storage/" + #census.censusfile.path
Not sure if this is at all the way to address this. If it is, is there a way to ask Paperclip where it is storing the files??
I've read where I could use:
Paperclip.io_adapters.for(#census.censusfile).path
But that seems to read the file into an array unless I'm missing something completely. My goal is to be able to loop threw a text file as well as feed an Excel file to axlsx for processing. I would also like to be able to eventually feed these files directly to a user somehow to allow for secure downloads.
I have looked hard for some documentation on all this and haven't found anything that really explains it yet. I'm to that point of just randomly throwing code here or there and hoping something works, which rarely does. Any help/direction that could be provided would be GREATLY appreciated!!!
Mark
I think io adapter can support read
Paperclip.io_adapters.for(#census.censusfile).read
so
<% Paperclip.io_adapters.for(#census.censusfile).read %>
<%= line %>
<% end %>
Use the copy_to_local_file method. This returns a file object on which you can read like on a normal file`.
I am using paperclip and AWS for my Rails app to upload image. You can find it here: http://lit-stream-6263.herokuapp.com/
When I try to upload images, I don't get an error but for some reason the image doesn't show. When I go into the S3 bucket though, I'm able to see the image that gets uploaded...it's just not rendering in the html page. Any advice on how to fix this?
Update
From 9nonnatus, I'm seeing the picture if I change the URL. However in my rails view I have
<%= image_tag product.avatar.url(:medium) %>
to display the image. This is what I see in the documentation as well. How do I adjust this to fit the url you mention above?
class Product < ActiveRecord::Base
attr_accessible :blog_link, :blog_name, :description, :image_link, :name, :num_likes, :product_link, :avatar
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
end
Looking at the source of the page you can copy the image link and try to access it in your browser. It gives an error telling you that your hyperlinks are incorrectly formatted. Instead of making the img src format something like:
http://s3.amazonaws.com/rockywolfugc/products/avatars/000/000/003/medium/59577_10100258833612183_1508749_n.jpg?1386876682
you have to use this format:
http://rockywolfugc.s3-us-west-2.amazonaws.com/products/avatars/000/000/003/medium/59577_10100258833612183_1508749_n.jpg?1386876682
In other words, remove /rockywolfugc from after .com and replace s3 with rockywolfugc.s3-us-west-2
Hope that helps.