I'm working with Ruby on Rails to develop an application and I'm really struggling with the following issue.
I have setup Cloudinary http://cloudinary.com/documentation/rails_integration and also Attachinary https://github.com/assembler/attachinary for users to upload a profile image using the rails application, I also want to be able to load these images in the Rails application to show the user's profile image.
I have setup the following code in the user.rb Model file
has_attachment :avatar, :accept => [:jpg, :png, :gif]
The edit page for User's comes under the Devise view in my application - edit.html.erb. The following form is what I'm using to upload User avatars/profile images:
<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :post }) do |f| %>
<%= cl_image_upload_tag(:avatar) %>
<%= f.button :submit %>
<% end %>
In the same View file I'm using the following code to retrieve the profile image:
<div class="profile-image" align="center">
<% if #user.avatar.present? %>
<img src="<%= cloudinary_url(#user.avatar.path) %>" width="180px" height="180px" alt=""/>
<% else %>
<img src="<%= asset_path "person.png" %>" width="180px" height="180px" alt=""/>
<% end %>
</div>
Upon the image being uploaded to Cloudinary it is given a random file name, for example: ecwc8x4ult960jzk8dp0
Am I uploading the image correctly and how do I retrieve them?
In the form try: <%= user.attachinary_file_field :avatar %>.
You should be able to retrieve them through the model like this: <%= image_tag user.avatar.url if user.avatar? %>, or like it's said in the Attachinary's documentation: <%= cl_image_tag(#user.avatar.path, { size: '180x180', crop: :thumb, gravity: :face }) %>. Omit the <img> tag and I suggest to not use inline css.
You could have 400 error for serving the images over HTTP instead of HTTPS. To fix that you should add secure: true to the cloudinary.yml or add :secure => true to the image tag.
I can't write comments... Are there any errors, could you post error logs?
Related
I am currently trying to get image uploading to work on Rails 4. On my development server when I upload a file it only displays on the page as a string of text, and I get the following error in my console:
identify: no decode delegate for this image format `' # error/constitute.c/ReadImage/562.
[2019-12-10 13:35:01] ERROR Dragonfly::FunctionManager::UnableToHandle: None of the functions registered with #<Dragonfly::Encoder:0x007f92df90d1d8> were able to deal with the method call encode(<Dragonfly::TempObject pathname=#<Pathname:/Users/me/projects/boss/public/system/dragonfly/dev,:jpg). You may need to register one that can.
I have tried reinstalling ImageMagick and the delegates but I have observed no difference.
Here is the code for the display and the upload.
<%= image_tag #person.photo.jpg.url %>
<%= form_for #person, :html => {:multipart => true} do |f| %>
<div class="form-group">
<%= f.file_field :photo %>
</div>
<% end %>
Here is the current output:
I can store the external image URL from the API response, and display it in view. But I am having trouble understanding how to store the the actual image so I can display it without calling the full external URL each time.
These are some of the resources I've looked at: Rails: upload a file OR store a url and http://code.runnable.com/UnsMH7fYN2V6AAAT/how-to-save-images-by-url-with-paperclip-for-ruby-on-rails-and-upload.
This is what I have so far:
# this is used for file upload from create form
has_attached_file :ephoto, :styles => { :medium => "480x270#", :small => "240x135#" }, :default_url => "/images/:style/missing.png"
if group_response.nil?
else
group_response.each do |group|
# stores external image URL into "ephoto_file_name"
event.update_attribute :ephoto_file_name, group["group_photo"]["photo_link"]
event.ephoto = open(group["group_photo"]["photo_link"])
end
end
The event.ephoto = open(group["group_photo"]["photo_link"]) does not seem to work properly. Using <%= image_tag event.ephoto.url %> just returns broken images.
Display image in view:
<% #events.each do |event| %>
<% if event.ephoto.path.present? && File.exist?(event.ephoto.path) %>
<%= image_tag event.ephoto.url(:medium) %>
<% else %>
<%= image_tag 'event-placeholder.png' %>
<%= image_tag event.ephoto_file_name %>
<% end %>
<% end %>
The <%= image_tag event.ephoto_file_name %> just uses the external URL each time to display the image instead of a stored image.
Is there a better way to display images from API responses? Any suggestions or insights would help. Thanks!
Hi I'm currently making an app for users to upload pics. I'm trying to add Amazon S3 in preparation to deploy my app. I'm not getting any error, but after trying to set up Amazon S3 my pics aren't uploading properly. The pics are uploading properly onto Amazon, not in my database so that when I upload the pics and try to loop through them in the views to show them, nothing shows. Can anyone assist me?
Just as a sidenote, I'm using Paperclip + jQuery file upload (for multiple pic uploads) + Amazon S3 + a s3_direct_upload gem (https://github.com/WayneHoover/s3_direct_upload)
config routes (notice that pics is nested in albums and users) no issue here
Pholder::Application.routes.draw do
resources :users do
resources :friends
resources :albums do
resources :photos
end
end
end
users/show.html.erb (the page that is supposed to show my uploaded pics, no new pics are showing up even after my uploads)
<% #user.albums.each do |album| %>
<div class="albumthumb">
<%= link_to image_tag(!album.photos.empty? ? album.photos.first.avatar.url(:medium) : ("questionmark.png"), :size => "150x150"), user_album_path(#user, album) %>
<br>
<%= album.name %>
</div>
<% end %>
photos/new.html.erb (issue here as well)
#my new uploader that works with amazon s3, but doesn't seem to be storing the pics in my database
<%= s3_uploader_form post: user_album_photos_path, as: "photo[avatar]", id: "myS3Uploader" do %>
<%= file_field_tag :file, multiple: true %>
<% end %>
#this is my old upload form that worked fine when uploading pics to my local public directory.
#
#<% provide(:title, "Upload pictures") %>
#
#<%= form_for([#user, #album, #photo], :html => { :multipart => true }) do |f| %>
#
#<%= f.file_field :avatar, multiple: true, name: "photo[avatar]" %>
<div id="pics">
<%= render :partial => "photo", :collection => #photos %>
</div>
<script id="template-upload" type="text/x-tmpl">
<div class="upload">
{%=o.name%}
<div class="progress"><div class="bar" style="width: 0%"></div></div>
</div>
</script>
Here's my code on github: https://github.com/EdmundMai/railstest/blob/master/app/views/photos/new.html.erb
Let me know if you need me to upload any more code. Please help!
JQuery file upload just uploads the files to Amazon S3: it doesn't add the required Paperclip records to the database. To do that, you need to do a few more steps. See the jquery-fileupload-rails-paperclip-example or jQuery File Upload v4 for Rails 3.
ive been trying to render a picture from carrierwave. i believe it is uploaded correctly because when i view the page source, i see
<img alt="Photo_44" src="/uploads/user/image/59/Photo_44.jpg" />
however by clicking on that src url, i get
No route matches [GET] "/uploads/user/image/59/Photo_44.jpg"
im using the default settings for carrierwave. the image does correctly get uploaded to my image column in my users table and locally, the path
sasha/Desktop/rails_projects/myproject/public/uploads/user/image/59/Photo_44.jpg
exists as well. however it won't display correctly. ive been following the railscasts
http://railscasts.com/episodes/253-carrierwave-file-uploads?autoplay=true
and reading
https://github.com/jnicklas/carrierwave
but i cant seem to figure out what is wrong. where i upload the image is here
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name),
:html => { :method => :put, :multipart => true }) do |f| %>
<%= devise_error_messages! %>
<%= render '/shared/fields', object: f.object, f: f %>
<%= f.submit "Save changes", :class => "btn btn-large btn-primary", :style =>"display:block;" %>
<div class="edit_avatar" >
<%= f.file_field :image %>
</div>
<% end %>
and im trying to render it by...
<%= image_tag #user.image_url.to_s %>
what am i doing wrong?
help would be appreciated = )
thank you
Try set config.serve_static_assets = true if you haven't yet, should help.
I'm using paperclip to upload images to the site but when I do, it shows the image placeholder with a question mark, not the image itself. What Am I doing wrong?
In the model:
attr_accessor :photo_file_name
attr_accessor :photo_content_type
attr_accessor :photo_file_size
attr_accessor :photo_updated_at
attr_accessible :photo
#paperclip-------------------------------
has_attached_file :photo,
:url => "/:attachment/:id/:style/:basename.:extension",
:path => ":rails_root/public/:attachment/:id/:style/:basename.:extension"
In the view:
<p id="notice"><%= notice %></p>
<div id="container">
<div id="content">
<%= image_tag #post.photo.url %>
<p>
<b>Title:</b>
<%= #post.title %>
and in the form I have:
<div class="field">
<%= post_form.file_field :photo %>
</div>
Get rid of all the accessor stuff and make sure your form returns something like:
params[:post][:photo]
Typically via:
form_for #post do |f|
f.file_field :photo
end
Then, in your posts controller, you can update or otherwise save a particular post with a :photo attached. Display the photo by simply tagging it image_tag #post.photo, Paperclip can do the rest.
I highly recommend removing your :url and :path options and just use the default for now, which is fine for most applications. It includes the system/ directory that is symlinked as shared by default under Capistrano, which is nice.
Did you check whether the image file is stored :rails_root/public/:attachment/:id/:style/ yet?
Right click to the image place holder to check the url of the image, is it right?