I have a some problems. And I know, what i'm not alone on it. I'm trying to use carrierwave and minimagic to add an images on my project (blog), but its return this: Image translation missing:
"ru.errors.messages.mini_magick_processing_error"
I allready instelled ImageMagick on my machine, and after that install the gems.
My form:
<%= bootstrap_form_for #post, :html => { multipart: true } do |f| %>
<div class="form-group">
<%= f.file_field :image %>
</div>
And my model:
class Post < ApplicationRecord
mount_uploader :image, ImageUploader
validates :title, :summary, :body, presence: true
end
I never used this gems before, and now i really don't know what to do. I install a last version of ImageMagic. I heard that its a popular problem on Windows. May somebody can help a student?
Screenshot of the error
see this similar issue and this (specifically for windows, you might need that package)
Related
I have a form for a blog, and I would like to have two field for images. One image being the cover (in Show) and another image will serve as a preview (in index).
My form looks as follow:
<%= semantic_form_for #blog, :html => { :multipart => true } do |f| %>
<%= t :Choose_File_for_cover %> <%= f.file_field :image_path, id: "avatar-upload2", required: true %>
<img id="img_prev3" width="100%" height=200 src="#" alt="your image" class="img-thumbnail hidden"/>
<%= t :Choose_File_for_homepage %> <%= f.file_field :homepagepic, id: "avatar-upload3", required: true %>
<%= f.hidden_field :image_path_cache %>
<%= f.hidden_field :homepagepic_cache %>
<%= f.actions do %>
<%= f.action :submit, :as => :input %>
<% end %>
<% end %>
My model looks like:
class Blog < ApplicationRecord
belongs_to :user
acts_as_taggable
mount_uploader :image_path, BlogUploader
mount_uploader :homepagepic, BlogcoverUploader
end
It works well when I only have the image_path (the cover), but when I add a new field for homepagepic, i get a ROLLBACK at validation.
Can someone help me on how to select files through two separate fields on the same form please.
Thank you
The code you've provided is very sparse and it would be helpful to see a little bit more (e.g. the controller and the uploader).
I can, however, hazard a guess: image_path is an existing helper method provided by Rails (see https://api.rubyonrails.org/classes/ActionView/Helpers/AssetUrlHelper.html#method-i-image_path). I have absolutely no idea what happens when you use this as a name for a form field. It could also be because you declare your submit button to be an input (I've only ever seen and used as: :button for f.action :submit).
So overall, I would pick the following approach:
rename your upload fields to cover_image and the other one to preview_image (that's what you've described in your posts as their respective purpose, so you should name them accordingly)
change the submit to a button and remove all the noise from your template and start with the bare minimum: the two upload fields and nothing else (see sample code below – note that I haven't tested it but it should work or be very close to working)
after that works, start adding back the noise (i.e. the translations, the cache fields etc.)
Test that it still works after every step. If you can write a Capybara test, do that – otherwise test it manually.
If you have questions, feel free to ask.
<%= semantic_form_for #blog, html: { multipart: true } do |f| %>
<%= f.file_field :cover_image %>
<%= f.file_field :preview_image %>
<%= f.actions do %>
<%= f.action :submit, as: :button %>
<% end %>
<% end %>
class Blog < ApplicationRecord
belongs_to :user
acts_as_taggable
mount_uploader :preview_image, BlogUploader
mount_uploader :cover_image, BlogcoverUploader
end
As the previous poster said it's hard to debug your code without all the pieces to the puzzle. A ROLLBACK is happening because one or more validations failed.
Any time you have a ROLLBACK you can add a ! to the create or update method being called on the object being rolled back and ActiveRecord will throw an error telling you why the ROLLBACK happened instead of failing gracefully.
Once you know why your object isn't persisting you can check the params of the controller action that form is submitting to. Perhaps you forgot to whitelist a param via strong params?
It's me again. I try to upload some yaml files with carrierwave. Everything works fine till now.
So, as you know for carrierwave the forms looks like the follow:
<%= form_for #resume, html: { multipart: true } do |f| %>
<%= f.label :name %><br>
<%= f.text_field :name, :required => true %>
<%= f.label :attachment %><br>
<%= f.file_field :attachment, :required => true %>
<br><br>
<%= f.submit "Save", class: "btn btn-primary" %>
<% end %>
What i want to do now is to remove the "name" field. I don't need it. So i thought its quite easy, just remove the "name" part of the form. But then I got an error while upload:
Name can't be blank
So I tried now nearly everything... I had set the required => false same result.
I went to Github and tooked a look at their how-to... there are methods to overwrite the name, but nobody cares about upload a file without a name. May somebody can tell me how i can upload a file without this name field?
Thanks!
Edit:
My resume.rb model:
class Resume < ActiveRecord::Base
mount_uploader :attachment, AttachmentUploader # Tells rails to use this uploader for this model.
end
My AttachmentUploader:
class AttachmentUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def extension_white_list
%w(yml)
end
def filename
"something.jpg" if original_filename # This is the part where i'm trying around right now.
end
end
Try to remove column name on table resumes and others related,
maybe on views, controller (strong params), migration file...
Then re-run drop, migrate database
I am getting an error when trying to upload images to my (listings) using the paperclip gem. The error that the browser outputs is: 1 error prohibited this listing from being saved: Image has contents that are not what they are reported to be **As a note, image magic has been successfully installed on my computer and there are no issues there
my listing.rb file
class Listing < ActiveRecord::Base
has_attached_file :image, :styles => { :medium => "200x", :thumb => "100x100>" }, :default_url => "default.jpg"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end
my gemfile
gem "paperclip", "~> 4.3"
my listings_controller
def listing_params
params.require(:listing).permit(:name, :description, :price, :image)
end
end
and finally my form
<%= form_for #listing, :html => { :multipart => true } do |f| %>
...
...
<div class="form-group">
<%= f.file_field :image, class: "form-control" %>
</div>
<div class="form-group">
<%= f.submit class: "btn btn-primary" %>
</div>
<% end %>
It sounds like you need to include file on your system.
If you're using Windows, you need to download file from this URL, install it on your hard drive and then add it to your PATH environment var:
Click "Start"
On "Computer", right-click and select "Properties"
In Properties, select "Advanced System Settings"
Click the "Environment Variables" button
Locate the "PATH" var - at the end, add the path to your newly installed file.exe (typically C:\Program Files (x86)\GnuWin32\bin)
Restart any CMD shells you have open & see if it works
You're probably trying to attach a file that is not properly recognised as an image, or one that has an image extension and has different content (like a PDF, for example).
Some workarounds are discussed here: https://github.com/thoughtbot/paperclip/issues/1924
It might help to check the log file as well - it should tell you what Paperclip thinks the type of the attachment is.
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?
I'm trying to write a rails application where users can upload images, but Paperclip doesn't seem to be working for me.
I've gone through all the basic steps (added has_attached_file, the migration, making the form multipart) but I keep getting the same error whenever I try uploading an image:
can't convert nil into Integer
Looking at the top of the stack
...rails3/lib/paperclip/processor.rb:46:in `sprintf'
...rails3/lib/paperclip/processor.rb:46:in `make_tmpname'
.../ruby-1.9.2-head/lib/ruby/1.9.1/tmpdir.rb:154:in `create'
.../ruby-1.9.2-head/lib/ruby/1.9.1/tempfile.rb:134:in `initialize'
It seems the problem is in the tempfile.
My code:
_form.rb
<%= form_for #high_school, :html => {:multipart => true} do |f| %>
<%= f.error_messages %>
...
<div class="field">
<%= f.file_field :photo %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
model/high_school.rb
...
validates_length_of :password, :minimum => 4, :allow_blank => true
has_attached_file :photo
has_many :students
...
Is this a known problem? I basically followed the instructions from the github to the letter.
My environment: Rails3 and Ruby 1.9.2dev
Thank you!
I don't believe paperclip supports ruby 1.9 yet.
Try dropping down to ruby 1.8.7 and see if the problem still exists.
you have to apply this patch http://github.com/dwalters/paperclip/commit/2a85add5f102db8773518f8ac30f7e2337bf7d13 to get paperclip working on 1.9.2 head for the can't convert nil into Integer error
This has now been fixed in version 2.3.3 of paperclip. See this commit:
https://github.com/thoughtbot/paperclip/commit/1bcfc14388d0651c5fc70ab9ca3511144c698903
Here somebody has similar problem to yours with Paperclip on ruby 1.9 (but there is no solution). So probably it is problem with ruby version.