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:
Related
I recently noticed that on my form, if I try to upload an empty file the page will get redirected to edit instead of create. If I try to upload the file with some text in it, the form will direct to create. I couldn't find any indication that this would happen as I create my object every time (it's never persisted). Is there an explanation behind this?
The code looks something like this:
//controller
def upload
#new_cool_file = CoolFile.new
end
//form in upload.html.erb
<%= form_for #new_cool_file, html: {role: "form"} do |f| %>
<div class="form-group">
<%= f.label :file %>
<%= f.file_field :file %>
</div>
<%= f.submit "Submit"%>
<% end %>
I think you do some wrong in the rails routes.rb file , can you please see tutorial about file upload.
https://richonrails.com/articles/allowing-file-uploads-with-carrierwave
http://railscasts.com/episodes/253-carrierwave-file-uploads
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?
So, I'm using carrier wave to allow users to upload profile images. Now ultimately, I'd like the user to be able to click on the image and upload a new one on the fly in the profile.html.haml page.
So, I've tried the below adding a form with a put action to let the user update the image on the fly without having to go to the edit.html.haml page.
However, I'm being hit with this error:
Could not find a valid mapping for nil
Extracted source (around line #8):
<%= form_for(#user, :url => registration_path(#user), :html => { :method => :put }, :html => {:multipart => true}) do |f| %>
<%= link_to (image_tag current_user.profile_image_url(:thumb).to_s), edit_account_path(current_user)%>
<% end %>
Where did I go wrong?
Alternatively I've tried this which might be totally off:
<%= link_to (image_tag current_user.profile_image_url(:thumb).to_s), f.file_field :profile_image%>
which gave this error:
Sites/Friendiose-master/app/views/home/landing_welcome.html.erb:9: syntax error, unexpected tSYMBEG, expecting ')'
...l(:thumb).to_s), f.file_field :profile_image);#output_buffer...
Thanks in advanced.
So firstly, what do you think this error might me. Most common errors ever:
Sites/Friendiose-master/app/views/home/landing_welcome.html.erb:9: syntax error, unexpected tSYMBEG, expecting ')'
...l(:thumb).to_s), f.file_field :profile_image);#output_buffer...
I'm not going to give you full answer but teach you to think how you can debug it.
https://github.com/carrierwaveuploader/carrierwave
That is the documentation for carrierwave. When showing the image it says the following:
<%= form_for #user, :html => {:multipart => true} do |f| %>
<p>
<label>My Avatar</label>
<%= image_tag(#user.avatar_url) if #user.avatar? %>
<%= f.file_field :avatar %>
<%= f.hidden_field :avatar_cache %>
</p>
<% end %>
One tip and this is not me being sarcastic but get used to reading alot and googling even more. I haven't used paperclip but I can see the issues from 2 mins googling.
Ruby on Rails using link_to with image_tag
There is how to create a link with an image.
From that link look at the difference between yours:
<%= link_to (image_tag current_user.profile_image_url(:thumb).to_s), edit_account_path(current_user)%>
and the correct way:
<%=link_to( image_tag(participant.user.profile_pic.url(:small)), user_path(participant.user), :class=>"work") %>
What does the error say you are missing?
It is not always great to give people an answer straight away but the keys on how to figure it out.
I'm uploading a plain text file and I'd like to use the text within as the parameters for a new object. Here's my attempt so far, in views/scans/new.html.erb:
<h1>New scan</h1>
<%= form_for :file_upload, :html => {:multipart => true} do |f| %>
<p><%= f.file_field :raw %></p>
<p><%= f.submit "Upload" %></p>
<% end %>
raw is a text attribute of the scan model. This yields the error:
Routing Error
No route matches [POST] "/scans/new"
Try running rake routes for more information on available routes.
I can tell from Google that I need to File.read() but I don't know where I would do it.
in model
def raw= (file)
File.open(file,"r") do |f|
self.your_atrribute = f.readlines.join("")
end
end
I have CarrierWave (0.6.1) and Nested Form gem installed. I have a Resource model with many Attachments which have a FileUploader.
I have a nested form where users can upload multiple files with one resource. I am following the section on github (https://github.com/jnicklas/carrierwave) that says how to make uploads work across redisplays unfortunately it's only for 1:1 ratio.
Here's the code that I have:
<%= nested_form_for #resource, :html=>{:multipart => true } do |f| %>
<p>
<%= f.label :title %>
<%= f.text_field :title %>
</p>
<%= f.fields_for :attachments, #attachment do |attachment_form| %>
<p>
<%= attachment_form.label :file %>
<%= attachment_form.file_field :file %>
<%= attachment_form.hidden_field :file_cache %>
<%= image_tag(attachment_form.file_url) if attachment_form.file? # DOESN'T WORK!!! %>
</p>
<%= attachment_form.link_to_remove "Remove this attachment" %>
<% end %>
<%= f.link_to_add "Add attachment", :attachments %>
<p><%= f.submit %></p>
<% end %>
Everything works and it populates the file_cache variable just fine for the attachment_form however I somehow need to add the following line in there to show the user the image of the document:
<%= image_tag(attachment_form.file_url) if attachment_form.file? %>
However there's a number of problems with this. First of all attachment_form is referencing the form_builder whereas I want the actual attachment. Second, attachment knows nothing about file.
Probably need to use another type of looping mechanism, but I'm new to Ruby so any help is appreciated.
Thanks all!
If you try this:
<%= image_tag(attachment_form.object.file_url) if attachment_form.object.file? %>
You will be able to show previous uploaded images. But if you want to display uploaded right now, you need to use something else. For example: https://github.com/blueimp/jQuery-File-Upload/wiki/jQuery-File-Upload-v4-for-Rails-3
Sorry, if I misunderstood your question.