Dragonfly adding images to store before Model.save - amazon-s3

The Dragonfly doc implies that nothing is written to the datastore until the model is saved:
When the model is saved, a before_save callback persists the data to
the App's configured datastore
Using Rails and Dragonfly 0.9.12, I have a model Article with image_accessor :image, and my store is S3.
The following is sufficient to store an image in S3:
Browser uploads image or provides image_url.
New model instance: #article = Article.new(params[:article]))
That's it. If I add processing via after_assign :resize_image, then the processed version is stored.
Is this behavior intentional? It's causing stray images to be uploaded to S3. I don't always save #article to db, but I still need Dragonfly to resize the image. (If you're curious, I'm sending it back to the browser for a preview as embedded data using Base64.encode64(#article.image.data).)
To investigate, I disconnected my internet to interrupt the store operation. This is the relevant portion of the trace:
dragonfly (0.9.12) lib/dragonfly/active_model_extensions/attachment.rb:179:in `store_job!'
dragonfly (0.9.12) lib/dragonfly/active_model_extensions/attachment.rb:118:in `retain!'
dragonfly (0.9.12) lib/dragonfly/active_model_extensions/class_methods.rb:63:in `block (3 levels) in register_dragonfly_app'
mongoid (3.0.3) lib/mongoid/attributes/processing.rb:102:in `process_attribute'
mongoid (3.0.3) lib/mongoid/attributes/processing.rb:27:in `block in process_attributes'
mongoid (3.0.3) lib/mongoid/attributes/processing.rb:25:in `each_pair'
mongoid (3.0.3) lib/mongoid/attributes/processing.rb:25:in `process_attributes'
mongoid (3.0.3) lib/mongoid/document.rb:147:in `block in initialize'
mongoid (3.0.3) lib/mongoid/threaded/lifecycle.rb:84:in `_building'
mongoid (3.0.3) lib/mongoid/document.rb:142:in `initialize'
How can I get around this? Thanks.

This is caused by using <%= f.hidden_field :retained_image %>.

Related

undefined method `featured' for Post

I try to add a full-text search in my app. With gem 'sunspot_rails', '~> 2.3' and gem 'sunspot_solr', '~> 2.3' for my app. When i try save something in mysql db like:
#post = Post.new
#post.title = "Theorema"
#post.save
It gives me ROLLBACK and:
NoMethodError (undefined method `featured' for #<Post:0x00007f018ab005d0>):
I am beginer and will be grateful for any help!
Welcome to SO Oleg.
Have you got an attribute featured in your Post model?
There is one on the github page of the gem you used.
Maybe you copied it by mistake. Either remove that if you do not need it or add it to posts and migrate.
Can not really tell without seeing your Post model.

Rails Rabl gem not showing line numbers

Whenever the rendering in rabl templates fails, it just shows the error. It does not show the line numbers. Has anyone faced this issue yet?
Example trace:
Rendered campaigns/show.json.rabl (1377.9ms)
undefined method `[]' for nil:NilClass
(eval):21:in `block (2 levels) in render'
/Users/pkhadloya/.rbenv/versions/1.9.3-p392/gemsets/myapp/gems/rabl-0.8.5/lib/rabl/builder.rb:87:in `call'
/Users/pkhadloya/.rbenv/versions/1.9.3-p392/gemsets/myapp/gems/rabl-0.8.5/lib/rabl/builder.rb:87:in `node'
Got this helpful link: http://davidsommers.com/2012/07/28/use-the-debugger-not-puts.html
It shows how to debug in rabl. But line numbers would have been more beneficial.

How to write RefineryCMS controller tests without RoutingError

An Rspec test like this (actually taken from RefineryCMS' own test suite)
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
module Refinery
describe FastController do
it "should render the wymiframe template" do
get :wymiframe
response.should be_success
end
end
end
Results in the following error:
Failure/Error: get :wymiframe
ActionController::RoutingError:
No route matches {:controller=>"refinery/fast", :action=>"wymiframe"}
# ./spec/controllers/fast_controller_spec.rb:6:in `block (2 levels) in <module:Refinery>'
In this case, I'm using refinery 2.0.8 with Rspec 2.11, and the relevant section after running rake routes looks like:
wymiframe GET /wymiframe(/:id)(.:format) refinery/fast#wymiframe
I have tried a few other controller Rspecs which also fail with routing errors. I am of course trying to write tests for my own extra methods I am adding to vanilla Refinery controllers, but just thought I'd see if I could get a controller test working for a totally fresh refinery install.
This must be a simple mistake! Any suggestions?
I stumbled upon this by accident. Instead of:
get :wymiframe
I needed to use:
get :wymiframe, { use_route: :any_old_thing }
I don't know why this worked - especially since for "any_old_thing", I really used nothing meaninful or connected tomy project at all. But, it seems to have worked, and now I can test my controllers.

Document upload (doc, docx, odt, etc.) and display in Rails (as text)?

Does anybody know how to upload a document to later show in a Rails application (as text)? Is Paperclip the right gem to do this? If it is how? (I have uploaded images before with Paperclip).
I like Paperclip. It seems well documented, and has worked well for everything I have needed. (I don't personally know any of them, but the clever folks at Thoughbot have created some pretty useful stuff, for which I feel indebted to them).
Obviously, you need to add Paperclip to your Gemfile, and (if you are using bundler) do your bundle install
Add to your model
has_attached_file :aFile
Add to you controller something to catch whatever you name it in your view (probably in your create and update methods)
#profile.aFile = params[:profile][:aFile]
Probably should check for its existence, if it is a required param
if params[:profile][:aFile].blank?
redirect_to #profile
else
render :action => 'do_something_interesting_with_file'
end
And that's about it. Don't forget your config entries. For example, if you are using some kind of post-processing on the file
Paperclip.options[:command_path] = "/opt/local/bin/"
I found this to be extraordinarily helpful
RailsCast by Ryan Bates

Rails validation on content type not working with Paperclip

I have a validation in my Rails (3.1.4) model to make sure no one tries to upload anything malicious in leiu of their profile image, but when I try to upload a jpeg, the validation is triggering. I'm using the Paperclip gem and I'm unsure if that is having an impact.
validation in user.rb model
validates_attachment_content_type :profile_image, :content_type => ['image/jpeg', 'image/png', 'image/gif'], :message => "Only jpeg, gif and png files are allowed for profile pictures"
When I look at the properties of the jpeg locally (Windows O/S):
Item type = JPEG image
Type of file: JPEG image (.jpg)
Am I doing something wrong in my validation?
Also, when it triggers, it puts the model and field name before the message. Is there a way to avoid that? ie 'Profile image profile image content type Only jpeg, gif and png files are allowed for profile pictures'
Thank you!
You should add 'image/jpg' to the content type array, I think that's what you're missing.