Report from backend to upload to s3 using carrierwave gem ruby - ruby-on-rails

I have Axlsx::Package.new object which is used to create a excel file in backend and now i have to upload this excel file to s3 using carrierwave gem. I have either file path or Axlsx::Package.new object but in both case it is throwing file can't be blank.. I am not sure what I am doing wrong.
Please help me how can I achieve this.
class Attachment < ApplicationRecord
# CarrierWave
mount_uploader :file, AttachmentUploader
validates :file, presence: true
end
when I execute this line, where I don't have ActionDispath::FileUpload object as i am creating file in backend not from UI form. due to this I have
file = Axlsx::Package.new
file_path = "uploads/exports/download_request_#{current_datetime}.xlsx"
attachment = Attachment.create!(file: file, entity_type: self.class)
i tried with file also and file_path also but it is not working and throwing file can't be blank...
class Attachment < ApplicationRecord
# CarrierWave
mount_uploader :file, AttachmentUploader
validates :file, presence: true
end
it should save to s3 as how it is working in case when i upload file from UI form using input type file tag.
Please help

Related

Carrierwave object.url VS object.image_url

In my Rails 5 app I am using Carrierwave to upload images.
I have to model that uses the same uploader:
account.rb:
mount_uploader :logo, ImageUploader
image.rb:
mount_uploader :image, ImageUploader
This uploads the file to:
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
The strange this now is that I can use:
#account.logo&.url(:thumb) // works!
#account.logo&.image_url(:thumb) // error!
But on the image model (one product has many images):
#product.images.first&.image_url(:thumb) // works!
#product.images.first&.url(:thumb) // error!
So in the first case I have to use .url and in the second one .image_url
An I have no idea why...any help?
The instance method image_url is defined dynamically based on the column that is passed to mount_uploader and simply calls url on the column. The definition looks like this...
def #{column}_url(*args)
#{column}.url(*args)
end
So, I would suspect that logo_url would work on #account (although I have not tested this)
source

Carrierwave remote attachment url not working on creation

I have the following model called ApplicationAttachment in my Ruby on Rails project.
I also have uploaded files on my s3 bucket. When i try to upload the image to my model, i dont get an error but attachment is nil and the remote_attachment_url is not saving the file. Not sure what is the problem.
My code is this
ApplicationAttachment.create!(remote_attachment_url: "http://www.jqueryscript.net/images/jQuery-Plugin-For-Fullscreen-Image-Viewer-Chroma-Gallery.jpg")
This doesnt return any error but it doesnt save the image.
class ApplicationAttachment < ActiveRecord::Base
mount_uploader :attachment, DeveloperReferralAttachmentUploader
attr_accessible :id, :attachment, :remote_attachment_url, :created_at, :updated_at
end
class DeveloperReferralAttachmentUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def extension_white_list
%w(pdf jpg jpeg gif png)
end
end
How do i ensure that the remote_image_url saves when created via Carrierwave.
Thanks
I think you might want to explicitly save the attachment instead of using create.
aa = ApplicationAttachment.new(
remote_attachment_url: "http://www.jqueryscript.net/images/jQuery-Plugin-For-Fullscreen-Image-Viewer-Chroma-Gallery.jpg"
)
aa.save

Retrieve file from S3. - Carrierwave Rails Gem

I'm implementing Carrierwave with fog storage into my Rails App. The whole purpose of this app is to store pdf articles and have the ability for anyone with access to the app to retrieve them. Right now, I have the functionality of storing the article pdf working great. But, now I need to be able to retrieve the pdf from S3. Is this possible? I noticed in the docs there is a uploader.retrieve_from_store!("my_file.png") method. I tried to run this in the console and I got this error NoMethodError: undefined method retrieve_from_store! for ArticleUploader:Class Any help with this would be great! I'm just not finding any suitable answers so far. Thanks!
Article Uploader
class ArticleUploader < CarrierWave::Uploader::Base
storage :fog
def extension_whitelist
%w(jpg jpeg gif png pdf)
end
end
Article Model
class Article < ApplicationRecord
mount_uploader :file, ArticleUploader
validates :title, presence: :true
validates :publication_date, presence: :true
validates :source, presence: :true
end
You can access it with the methods that carrierwave has, in your case:
article = Article.find(1)
article.file.url
If you are on development it will output the path for the file and if you are on production and using S3, it will output the whole url, http://s3.amazonaws.com/<vendor>/articles/1/file.pdf for example.
You can find more information on the official docs:
https://github.com/carrierwaveuploader/carrierwave#activerecord
There is also an old rails cast on that http://railscasts.com/episodes/253-carrierwave-file-uploads

Test failing for uploading documents fails in rails

I am trying to test for uploading documents in rails. I am using carrierwave to upload documents. The relevant code is given below.
Document Model Test Code
class DocumentTest < ActiveSupport::TestCase
def setup
#category = categories(:category_a)
#document = #category.documents.build(file_name: "abc", file: "abc.doc")
end
test "document valid" do
assert #document.valid?
end
Document Model Code
class Document < ActiveRecord::Base
belongs_to :category
mount_uploader :file, FileUploader
validates :file_name, presence: true
validates :file, presence: true
validate :file_size
end
file_uploader code
The following are the files I have whitelisted.
def extension_white_list
%w(pdf doc htm html docx xlsx xml)
end
I get a Failed assertion when I run the tests. I commented out the validates :file, presence: true code and the tests pass. I have file as type string in the database. However, passing in a string value is the same as passing a blank value. Therefore, the presence: true validation fails. I am not sure why that is failing even though I am passing in a string value. I think I need to pass in additional params or information for the tests to know what kind of file it is. Thanks!
This is not how you pass a file to the carrierwave model. Have a look at these examples:
https://github.com/carrierwaveuploader/carrierwave/wiki/How-to:-Use-test-factories
Also, carrierwave has its own validators you might want to use:
https://github.com/carrierwaveuploader/carrierwave/wiki/How-to:-Validate-uploads-with-Active-Record

save generated files with paperclip or carrierwave

how can I save generated xls (without uploading form) file with paperclip or carriervawe to ActiveRecord and use it then to upload on amazon s3?
file = Tempfile.new(['conracts','.xlsx'])
ToXls::Writer.new(content, {:columns => [:name, :address],
:headers => ["name","address"]}).write_io(file)
file.rewind
this is the xls file I generate , but i need to save it permanent in order to upload it with delayed job on s3.
With Carrierwave, you can assign a normal file to the mountpoint of the uploader, then save the model.
Carrierwave will then handle all uploading.
So if you have a model
class Project < ActiveRecord::Base
mount_uploader :xml_file, XmlUploader
end
you can write
pro=Project.new
pro.xml_file = file # your generated tempfile
pro.save
I'm not familiar with paperclip.

Resources