Why does paperclip uploaded image is not been displayed on rails 4? - ruby-on-rails

I was able to upload an image with paperclip.
I am now trying to show that image
<%= image_tag #profile.image.url %>
this renders
<img alt="Missing" src="/images/original/missing.png?1379966496">
And nothing is been displayed.
What am I doing wrong ?

In the model for profile be sure that you added:
attr_accessible :image
Paperclip will upload the file, but you need to whitelist it for it to be used in the view

Are you trying to edit this in a form? I had the same problem, but by having the following code in my
app/views/product(your resource name)/_form.html.erb
file fixed it:
<div class="field">
<%= f.label :avatar%><br>
<%= f.file_field :avatar %>
</div>

Related

Using Dropzone.js in a file_field within a Rails form

I am trying to use Dropzone.js to upload files within my Rails app.
It seems that if I use the standard setup, the entire form becomes an image upload field. However, my form contains other fields as well. I only want to use Dropzone.js in a file_field area.
Steps I've used are:
Gemfile
gem 'rails-assets-dropzonejs', source: 'https://rails-assets.org'
application.js
//= require dropzonejs
application.css
*= require dropzonejs
_form.html.erb
<%= form_for #activity, html: {class: 'ui form'} do |f| %>
<!-- Fields like this one don't need to be dropzone fields -->
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<!-- The following field does -->
<div class="field">
<%= f.label :gallery_images %>
<%= f.file_field :gallery_images, multiple: true, class: 'drop' %>
<%= f.hidden_field :gallery_images_cache %>
</div>
<% end %>
activities.coffee
$ ->
$('.drop').dropzone({ url: "/activities/post" });
As you can see, I'm trying to bind Dropzone to the 'drop' class which I've attached to the file_field. However, this doesn't seem to work correctly and I am seeing no errors in the console.
Anyone have an idea how I'd get Dropzone.js to work for a file_field within a Rails form? Or can I only bind it to the entire form?
Any help is much appreciated! Thanks!
You need to permit the :file param. Most probably there will be code something along the line of
private
def activities_params
params.permit(:name, ...other_params)
end
Add :file to the permit method
private
def activities_params
params.permit(...other_params, :file)
end

Thumbnail of image won't show up until I submit the form (carrierwave and cocoon)

This is the code in _image_fields.html.erb
<div class="nested-fields">
<div class="image-fields"> <%= image_tag f.object.image_url(:thumb) if f.object.image_url.present? %>
<%= f.file_field :image %>
<%= f.hidden_field :image_cache %> </div> <div class="add-language-spacing"> <%= link_to_remove_association 'Remove Image', f, id: 'remove_photo' %> </div>
This is where i'm rendering that partial in _profile_form.html.erb
<p id="tag">Add Some Photos!</p>
<div>
<%= f.fields_for :images do |image| %>
<%= render "image_fields", f: image %>
<% end %>
</div>
And then finally, in profile/edit.html.erb
<div class="profile-form">
<h1 class="page-title">Update Profile</h1>
<%= render 'profile_form' %>
</div>
My uploader looks like this
# Create different versions of your uploaded files:
process resize_to_fit: [300,300]
version :thumb do
process resize_to_fill: [200,200]
end
The thumb version only shows up after I press update profile and then refresh the page. For whatever reason it won't show the thumbnail version when I press choose file.
This is the confusing part of webdevelopment when you are starting out: what is executed in the client (browser) and what is executed on the server.
1) You are using cocoon to build a form, this means all data inside the form is only processed on the server once you submit the form.
2) You are using carrierwave, which explicitly only works in ruby on the server.
So together this means: only after submitting the form to the server, your uploader will create the thumbnail, and thus only after submitting the image_url will be defined.
What you want is, once you select the image, it will show a thumbnail, before uploading it to the server, and this means you either need
ajax to immediately post to the server (and let it render the thumbnail)
a javascript solution, which will read the contents of the file and show it (client-side, in the browser), before sending it to the server
Personally I do not believe cocoon is the right tool for the job here. I would suggest using something like
jquery-file-upload
dropzonejs

uploading empty file goes to edit instead of new

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

Paperclip images not Displaying in rails app

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?

Problems using attribute_fu to update a record

I'm using attribute_fu and paperclip to add multiple images to a specific record.
This is working great when creating a new record but when I want to add new images to an existing record through a common scaffold update, the images upload but replace the existing images.
Any thoughts or advice?
I'd found maybe the problem.
my model upload has article_id, description, photo_file_nane
In my partial: _upload.rhtml
if I just put:
<div class="upload">
<%= f.file_field :photo %>
<%= f.remove_link "remove" %>
</div>
when I update, it delete my photos.
but if I put:
<div class="upload">
<%= f.text_field :description %>
<%= f.file_field :photo %>
<%= f.remove_link "remove" %>
</div>
It works fine

Resources