world!
After implementing Cloudinary's Active Storage Integration, it's easy to make it work in a form.
We just need to add to our simple_form_for a
<%= f.input :photo, as: :file %>
BUT, if we want to use the Cloudinary Upload Widget, there's no straightforward way of adding this file to the form.
What I have tried is:
1- let the widget upload the picture;
2- update the value of a hidden input with the url of the uploaded picture;
3- in the controller, when creating the model, download the picture and attach it to the model:
require 'open-uri'
file = URI.open(photo_url)
#user.photo.attach(io: file, filename: 'profile_photo.jpg')
# [...]
private
def photo_url
params.require(:user).permit(:photo_url)[:photo_url]
end
It works, BUT it uploads twice the same picture and - IMHO - is extremely inefficient.
Any ideas?
Thank you!
Good Luck, Have Fun!
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.
i am creating a rails application and i wanna user to be able to enter link for his/her profile picture from Facebook or any external site .
how to be sure this link is for image??
how to create a validation for that ??
Use this validation in model.
validates_format_of :image, :with => %r{\.(png|jpg|jpeg)$}i, :message => "Use a real image"
Please be aware that this would only validate that the filename would end in a specific string.
Use fastimage to get type and size of images by quickly fetching.
eg.
FastImage.type("http://stephensykes.com/images/pngimage")
returns .png
I am working on a site where I want to be able to display a default image for a user profile if they have not yet uploaded their own image.
I can place the default image in the asset/images directory, but I am wondering how I should make the logic work with image_tag to be able to display the user's image in the view if they've uploaded one or display the default placeholder image in the view if the user has not uploaded one.
I am new to rails, but I would guess that I should put the logic somewhere in my user model and conditionally set the image attribute for the user? I am using Paperclip as well.
The paperclip gem supports a default_url option that is a path to an image that will be rendered if the user has no image. From the paperclip README:
class User < ActiveRecord::Base
attr_accessible :avatar
has_attached_file :avatar, :styles => { :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
end
So if you call user.avatar.url(:thumb) and it is missing, paperclip will try to render /images/thumb/missing.png. If you create your default missing images and line them up with the default_url parameter, you should be able to let paperclip handle the rest.
I have two models: photos and properties. A property has_many photos. When uploading a photo via paperclip I want the directory structure to be something like properties/the_property_id/:id.:style (of course, the second :id here would be the image id since thats what model were in).
A photo is added and edited via the properties model (accepts_nested_attributes_for). So, while viewing the property edit page I need a way of passing the property id (params[:id]) to the photos model so paperclip can use something like :url => properties/params[:id]/:style/:id.:extension. (properties/6/small/2.jpg)
Looking for advice on the best way to do this... How would you accomplish this task?
You need to use Paperclip interpolations(see https://github.com/thoughtbot/paperclip/wiki/Interpolations for details). Then you can have something like this:
Paperclip.interpolates :param_value do |attachment, style|
attachment.instance.property.params[attachment.instance.id]
end
And use it in your model in such way:
:url => properties/:param_value/:style/:id.:extension