Paperclip and tempfile with Rails - ruby-on-rails

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.

Related

Errors when trying to display an image from ActiveStorage in Rails 5.2

I am working on a Rails app using Rails 5.2 and Ruby 2.3.7 and want to attach an image to my Event object, using Active Storage.
Here are the main steps I've taken
In config/environments/development.rb I confirmed that: config.active_storage.service = :local
Files
event.rb I've added this line:
has_one_attached :event_image
events_controller.rb I have the :event_image attribute whitelisted in event_params
events/_form.html.erb I have set a form_for to upload an image
<%= f.label :event_images %>
<%= f.file_field :event_image %>
events/index.html.erb I try to display the image with
<% if event.event_image.attached? %>
<%= image_tag event.event_image %>
<% end %>
Error: Can't resolve image into URL: undefined method `attachment_url' for :0x00007fca5dcef8b8>
<% if event.event_image.attached? %>
<%= image_tag url_for(event.event_image) %>
<% end %>
Error: undefined method `attachment_path' for Class:0x00007fca5e0109c0>:0x00007fca5dacefe8>
I have
confirmed that active_storage_attachments and active_storage_blobs exist in my database and the attachments are saved there
Any suggestions would be very much appreciated. From all my googling it would seem that this should work
When working with Spree, you should prefix the url_for method with: main_app.url_for:
image_path(main_app.url_for(event.event_image))
This is valid with Spree v3.6.5 and v3.6.6, not sure about other versions.
Further update on this issue. I repeated the same steps in my own application and it worked fine. This only occurs when working with an application that is using Spree
image_path(main_app.url_for(event.event_image))
It works for me on my spree 4.0 extension.

MiniMagic and Carrierwave on Rails (Windows)

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)

File uploading Mailboxer Gem

I'm trying to send a message with an attachment in Mailboxer Gem.
My stack is : Rails 4 and Ruby 2.1.1
However, I can see that attachment using CarrierWave is already supported as the link in the code below.
https://github.com/ging/mailboxer/blob/4b2681c1790b823f7b493fb00b41e9899bb90ebe/app/models/message.rb#L13
However, I did my setup exactly like that. Normal message without an attachment is going fine.
This is my code :
Controller :
def create_message
if params[:user].present? & params[:message].present? & params[:subject].present?
current_user.send_message(User.find(params[:user]), params[:message], params[:subject])
redirect_to inbox_path
end
end
This is my view code :
<%= form_tag do %>
<%= select_tag 'user', options_from_collection_for_select(User.all, :id, :fullname) %><br/>
<%= text_field_tag 'subject' %><br/>
<%= text_area_tag 'message' %><br/>
<%= submit_tag 'Send' %>
<% end %>
The above code is working fine and the messages are getting sent, however, when I try to add the file field to it like so and try changing the controller code, the attachment is not getting uploaded :
def create_message
if params[:user].present? & params[:message].present? & params[:subject].present?
current_user.send_message(User.find(params[:user]), params[:message], params[:subject], true , params[:attachment])
redirect_to inbox_path
end
end
View :
<%= form_tag do %>
<%= select_tag 'user', options_from_collection_for_select(User.all, :id, :fullname) %><br/>
<%= text_field_tag 'subject' %><br/>
<%= text_area_tag 'message' %><br/>
<%= file_field_tag 'attachment' %>
<%= submit_tag 'Send' %>
<% end %>
I think this is a problem with the strong params. In Rails 3 I could have used attr_accessible. However how do I ensure that the attachment field is not being blocked and allowed?
P.S - I have the carrierwave gem installed and I have even restarted my server multiple times.
Thanks.
I am changing my previous answer, because in fact it wasn't correct.
Basically, if your problem is like mine, it could easily be solved by adding :multipart => true to your form. At least in my case, that's why carrierwave was not picking up the attachments.
I tried to solve the problem by extending the mailboxer Message class and setting up and mounting a completely new carrierwave object with a different attribute name. Basically, this allowed me to avoid working with the attachment attribute defined in mailboxer and to customize the attachments.
But this got very messy with the extension of the mailboxer message.rb Class. So I finally abandoned that course. Still, having your own uploader instead of relying on the mailboxer attachment is very convenient, especially if you want or need to upload your files to a different directory or to the cloud.
So finally, I have created a new model for my attachments and mounted a new carrierwave uploader on it. In that way, I can customize it as I want without having to tweak mailboxer, which has very little in terms of documentation or support.
Probably this is not useful to you anymore, but might help others!

Error with nested_form gem: wrong number of arguments (4 for 3)

I have been struggling with this one for days and can't seem to figure out what's wrong. I'm attempting to allow polymorphic file attachments to a model Item, which belongs to model Location. My routes are defined as:
resources :locations do
resources :items
post :sort
end
resources :items do
resources :assets #model for attachments
end
I followed a tutorial about doing exactly this with carrierwave and nested_form. After setting everything up, however, I get the following error when requesting the New action for the Item model: wrong number of arguments (4 for 3). It tells me the error is occurring at line 7 of this view:
<%= nested_form_for [#location, #item], :html => { :multipart => true } do |f| %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<%= f.fields_for :assets do |a_form| %> ### LINE 7 ####
<p>
<%= a_form.label :file %><br />
<%= a_form.file_field :file %>
<%= a_form.hidden_field :file_cache %>
</p>
<%= a_form.link_to_remove "Remove this attachment" %>
<% end %>
<%= f.link_to_add "Add attachment", :assets %>
<p><%= f.submit %></p>
<% end %>
If I don't use the nested_form gem and start out the view with a normal form_for, I get no errors and am able to successfully attach a single file to the Item. I can try and proceed without the gem, but (as far as I understand) nested_form will automate some of the functionality like removing the files and generating ajax to add new attachments.
I was just wondering if anyone has run into this error or knows what mistake I'm making that's causing problems with nested_form? I understand what the error means, just not sure where/why the extra argument is being thrown in. I greatly appreciate any insight you can provide!
FYI my dev setup: rails (3.1.0, 3.0.10), nested_form (0.1.1), carrierwave (0.5.7)
In order to get nested_form working with rails 3.1, I had to pull the latest from github rather than using what's in the gem. In my Gemfile:
gem "nested_form", :git => "git://github.com/ryanb/nested_form.git"

Paperclip failing to upload on specific scaffold, yet works on others

I know there are tons of questions about paperclip, but I failed to find the answer to my problem.
I know its prob just something simple, but I I'm running out of hair to pull out.
I have paperclip working on other parts of my project, they work with no problem, however, a certain scaffold fails to upload, all the attributes to the uploaded file are nil.
Here are the relevant information.
Model:
has_attached_file :foo,
:styles => { :thumb => "140x140>" },
:url => "/data/:id/:style/:basename.:extension",
:path => ":rails_root/public/data/:id/:style/:basename.:extension"
View:
<% form_for(#bar, :html => { :multipart => true }) do |f| %>
<%= f.error_messages %>
----------
<li><%= f.label :top %>
<%= f.file_field :foo %></li>
----------
<ul><%= f.submit "Save" %></ul>
<% end %>
Also, comparing the logs to the parts that work, the :foo attribute seems to be passing different values than in the ones that work.
In the logs, when the paperclip function works, it looks like this
"image"=>#<File:/var/folders/M5/M5HEb+WhFxmqNDGH5s-pNE+++TI/-Tmp-/RackMultipart20100512-1302-5e2e6e-0>
when it does not, it seems to pass the file name directly
"foo"=>"foo_image.png"
I am developing locally on MacOSX using local rails and ruby libs.
Still haven't narrowed it down, but just to let people know, the problems went away after erasing all the views and regenerating them from scratch, meaning it was a view issue.
Will follow up.

Resources