refile
Ruby file uploads, take 3
I'm using Refile with a rails 4 app.
I'm getting the error: undefined method
accepts_attachments_for'`
I'm trying to do multiple image upload, and have two models: books and blobs.
My books.rb:
has_many :blobs, dependent: :destroy
accepts_attachments_for :blobs
My blobs.rb:
belongs_to :book
attachment :file
If I check rake routes, it shows that refile is mounted, so what is the issue?
Source: (StackOverflow)
I'm using Refile with Rails 4. I'm following their tutorial for multiple image upload. Each Post can have multiple Images. My models look like this:
Post.rb:
has_many :images, dependent: :destroy
accepts_attachments_for :images, attachment: :file
Image.rb:
belongs_to :post
attachment :file
I can upload files, fine by using:
<%= f.attachment_field :images_files, multiple: true, direct: true, presigned: true %>
but when I try to retrieve an image like:
<%= attachment_image_tag(@post.images, :file, :small) %>
I get the error:
undefined method file for #<Image::ActiveRecord_Associations_CollectionProxy:0x007fbaf51e8ea0>
How can I retrieve an image with refile using multiple image upload?
Source: (StackOverflow)
I have an issue regarding Refile and its multiple file options when I perform validation of my forms.
When I send a form with attachments (let's say "docs A"), and the form doesn't succeed in its validation (let's say because email was required), refile has the following behaviour:
- If no other files are attached, and we resubmit the form (a valid one this time), "docs A" are persisted and moved from the cache to the store folder (all done by refile). This is all good.
- If other files ("docs B") are included in the form, and the latter is submitted, only "docs B" will be considered (no matter if the form was valid, or invalid, and despite the :append value -- check the models below). If the form was valid, only "docs B" will be persisted. If the form is invalid, once we make it valid and resubmit it, it will persist "doc B" (unless we attach new files).
"docs A" and "docs B" could be single files as well. It will happen the same behaviour.
I've got the following models
class User < ActiveRecord::Base
has_many :documents, dependent: :destroy
accepts_attachments_for :documents, append: true, attachment: :file
end
class Document < ActiveRecord::Base
belongs_to :user
attachment :file, type: :document
end
And in the form:
<%= f.attachment_field :documents_files, multiple: true, direct: true, presigned: true %>
I'm using refile 0.5.5.
I've already tried to include hidden inputs
<% @user.documents.each do |doc| %>
<%= f.hidden_field "documents_files", multiple: true, value: {id: doc.file_id, filename: doc.file_filename, size: doc.file_size} %>
<%# end %>
But they get "overriden" if new attachments are included.
I was wondering if the multiple and append behaviour of refile are actually working, or whether my implementation is incorrect.
The expected behaviour is that new attachments should get appended to the existing ones (i.e. "docs A" + "docs B").
I'd appreciate any help!
Source: (StackOverflow)
My app has a listings table(:name, :description)
and a pictures table(:image_id, :listing_id)
. I am using a nested form(tabbed) for getting all this info and its working fine with gem carrierwave
. For the last couple of days I am trying to add amazon s3 direct image uploading feature to my rails application. But it seems like its hard to accomplish this in a nested form. So i am planning to list all image_url in the listing table itself. As its all just url's of the real image is it a good idea to do it this way? (i will only allow maximum 8 images per listing)??
I am planning to do this with refile if possible.
Looking for someone to shed some light into this topic...Thanks in advance for trying to help :)
Source: (StackOverflow)
I'm able to upload multiple images, but I'm not sure how to customize the size of the image.
This is how I view the images:
<%= attachment_image_tag(image, :file, :fill, 300, 300) %>
:fill
: crops the images
300, 300
: is the height and width
Before refile, I had hardcoded the source of the images, and then I used this CSS to fix the size of the image.
img {
display:block;
max-height: 430px;
max-width: 100%;
width:auto;
height:auto;
}
I wanted to have a height consistent throughout, but the width can be any size and won't be distorted or cropped.
At the time of this post, I was thinking that I can get away with increasing the width and height in the refile attachment_image_tag option to like 800, 800
, but it still crops the images... Also, I tried to get rid of :fill
, but my images won't show, and the code just breaks.
Source: (StackOverflow)
We are using refile to allow users to upload images to our S3 back end. In addition, we allow users the option of entering a URL to any image on the internet (through the remote_image_url
property.)
This works fine, as long as the URL entered is pointing to an actual file. However, in the case where there was a mistake in the URL, or some nonsensical input was given, Refile
will throw the following exception:
Errno::ENOENT (No such file or directory @ rb_sysopen - thiswillnotwork):
app/controllers/my/deals_controller.rb:17:in `create'
appsignal (0.11.2) lib/appsignal/rack/listener.rb:13:in `call'
Is there an option to ignore the cases where the URL entered is invalid (akin to how the validate_download
option in CarrierWave works) and, ideally, use our fall back image instead?
We have tried mounting the attacher with the raise_errors
option set to false
, but with the same results.
Our project uses Rails 4.2.0
and Refile 0.5.3
.
Edit:
I have confirmed that this exception is a lower level SystemCallError
, coming from Kernel.open
, and this exception type is not being rescued by Refile:
rescue OpenURI::HTTPError, RuntimeError => error
raise if error.is_a?(RuntimeError) and error.message !~ /redirection loop/
@errors = [:download_failed]
raise if @raise_errors
end
I am working on a pull request to refile to fix this.
Edit 2:
While working on this, we discovered a major security issue in Refile
, enabling a potential attacker to use remote code execution.
The Refile gem has a feature where a URL will be supplied and the remote file will be uploaded. This can be done by adding a field like remote_image_url
in a form, where image
is the name of the attachment. This feature was using open-uri to make this HTTP request without validating the passed URI. An attacker can craft a URI which executes arbitrary shell commands on the host machine.
If you are using Refile
versions 0.5.0
- 0.5.3
, please upgrade to the latest version. Upgrading will also solve the issue above.
Source: (StackOverflow)
I have a web app in Rails that has a profile image upload, I`m using Refile gem to upload the photo direct to Amazon S3.
But now I need to do this upload from a mobile app too.
Witch is the best way to do that?
I think about a Rest API in Rails that receives the binary data and then uses Async Jobs (Sidekiq) to upload to Amazon S3, but and not sure if the approach is best way.
Can anyone help me?
Thanks!
Source: (StackOverflow)
I'm looking at this documentation, and I'm having trouble coming up with a solution in creating checkboxes to my images, but associated to the form?
If you see below, my images are looped inside the form...
edit view
<%= simple_form_for @project, html: { multipart: true } do |f| %>
<% @project.project_images.each do |img| %>
<%= attachment_image_tag(img, :file, :fit, 1000, 430) %>
<% end %>
<%= f.attachment_field :trip_images_files, multiple: true %>
<%= f.button :submit %>
<% end %>
I can add <%= f.check_box :remove_project_images %>
inside the .each loop?
<% @project.project_images.each do |img| %>
<%= attachment_image_tag(img, :file, :fit, 1000, 430) %>
<%= f.check_box :remove_project_images_files %>
<% end %>
Actually, at the time of writing this.. I'm getting undefined method 'remove_project_images_files'
, now I'm not even sure at all.
Source: (StackOverflow)
it's my first Rails project with the Refile gem, so I don't fully grasp all the ins and outs of this wonderfull file upload gem yet.
I'm trying to configure the Refile gem so that files are uploaded to Amazon S3 in production mode, but in development mode files are uploaded to the file system.
my config/initializers/refile.rb file looks like this:
require "refile/backend/s3"
aws = {
access_key_id: Rails.application.secrets.s3_key,
secret_access_key: Rails.application.secrets.s3_secret,
bucket: "adlit",
}
Refile.cache = Refile::Backend::S3.new(prefix: "cache", **aws)
Refile.store = Refile::Backend::S3.new(prefix: "store", **aws)
end
but when I try to upload an image in development, I get this error message:
Missing Credentials. Unable to find AWS credentials. You can configure your AWS credentials a few different ways: * Call AWS.config with :access_key_id and :secret_access_key * Export AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY to ENV *...
So I tried to fix this by adding the configuration in a conditional:
if Rails.env.production
...
end
But that didn't fix it, I still get the above error message in development mode.
Does anyone know how I can configure Refile to upload file to Amazon S3 in production and to the file system when in development mode?
thanks for your help,
Anthony
Source: (StackOverflow)
I choose refile gem to handle file uploads in my application. I have some trouble with use it with FactoryGirl.
In my factory I have something like that:
include ActionDispatch::TestProcess
FactoryGirl.define do
factory :company do
sequence(:name) { |n| "Company #{n}" }
logo_id { fixture_file_upload(Rails.root.to_s + "/spec/fixtures/images/1x1.gif") }
end
end
When I run some feature specs it returns me the following error:
5) Company destroying destroys company
Failure/Error: visit companies_path
ActionView::Template::Error:
Refile::InvalidID
EDIT:
This error is caused by this line in my view template:
%td= image_tag attachment_url(company, :logo, :fill, 50, 50)
Source: (StackOverflow)
In dev my rails app works just fine.
In production instead there is just no image, it's uploaded to the server but it just won't show up. I'm using the default backend.
When accessing the image url directly all I see is "error". :(
Hope somebody could give me a hint.
Source: (StackOverflow)
I'm deploying a rails app to production using the refile gem for file uploads and connecting to s3 for storage. Everything works great in development I'm getting the following error in production when trying to upload a new image or retrieve an existing one:
E, [2015-01-30T16:59:02.841941 #29551] ERROR -- : Refile::App: Error ->
Missing Credentials.
Unable to find AWS credentials. You can configure your AWS credentials
a few different ways:
* Call AWS.config with :access_key_id and :secret_access_key
* Export AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY to ENV
* On EC2 you can run instances with an IAM instance profile and credentials
will be auto loaded from the instance metadata service on those
instances.
* Call AWS.config with :credential_provider. A credential provider should
either include AWS::Core::CredentialProviders::Provider or respond to
the same public methods.
= Ruby on Rails
In a Ruby on Rails application you may also specify your credentials in
the following ways:
* Via a config initializer script using any of the methods mentioned above
(e.g. RAILS_ROOT/config/initializers/aws-sdk.rb).
* Via a yaml configuration file located at RAILS_ROOT/config/aws.yml.
This file should be formated like the default RAILS_ROOT/config/database.yml
file.
E, [2015-01-30T16:59:02.842423 #29551] ERROR -- : Refile::App: /home/deploy/real_org/shared/bundle/ruby/2.1.0/gems/aws-sdk-v1-1.60.2/lib/aws/core/credential_providers.rb:140:in `credentials'
I have configured the credentials in the RAILS_ROOT/config/initializers/aws-sdk.rb file like so
require 'aws-sdk'
AWS.config(access_key_id: ENV["AWS_ACCESS_KEY_ID"], secret_access_key: ENV["AWS_SECRET_KEY"])
And I have confirmed that the env variables exist by typing
$: echo $AWS_SECRET_KEY
$: echo $AWS_ACCESS_KEY_ID
which returns the keys properly. Any ideas?
Source: (StackOverflow)