I'm trying to recreate the images that I have uploaded using the following in my model...
Post.all.each do |ym|
ym.avatar.cache_stored_file!
ym.avatar.retrieve_from_cache!(ym.avatar.cache_name)
ym.avatar.recreate_versions!
ym.save!
end
Unfortunately, I get the following error....
(undefined method `body' for nil:NilClass):
My uploader is named AvatarUploader and is for my Post model. Any advice on how to fix this?
I've ran with a similar problem when using async processing on a read-only server(Heroku). In my case there was a problem when defining file size which I resolved monkey-patching def size from fog file class:
module CarrierWave
module Storage
class Fog < Abstract
class File
def size
file.nil? ? 0 : file.content_length
end
end
end
end
end
I could help you more if the issue still persists and when you post more details from your backtrace and gems configuration.
Related
I've just updated my Gemfile.
At the beginning I thought problem came from Zeitwerk (from 2.4.2 to 2.5.4) but I've downgraded it and I still have an issue on my spec. I've isolated that the problem does not come from RSpec and dependencies.
Actually, RSpec does not found a class which is defined within another file and does not match the file name/class name.
Important point: Filter::MyStandardError is found.
# app/services/filter/my_standard_error.rb
module Filter
class MyStandardError < StandardError; end
class MySpecificError < MyStandardError; end
# ...
end
# app/services/filter/my_tested_service.rb
module Filter
class MyTestedService
def initialize
raise ::Filter::MySpecificError
end
end
end
RSpec.describe Filter::MyTestedService do
subject { described_class.new }
it 'raises an error'
expect{subject}.to raise_error(::Filter::MySpecificError)
end
end
And I got the error:
NameError:
uninitialized constant Filter::MySpecificError
I got the Changelog but breaking changes are not used on my configuration.
Does anybody have an idea for this one?
You do not need to add app/services to the autoload paths, that is done automatically by Rails. I'd suggest to remove that configuration to keep things simple/idiomatic.
The implementation of app/services/filter.rb should not be needed. Your application is doing something that is not right, we just need to find it.
Could you please delete app/services/filter.rb, throw Rails.autoloaders.log! in config/application.rb, trigger the error, and share the traces?
After reading one-file-one-constant-at-the-same-top-level
I found this to fix my issue
# app/services/filter.rb
class Filter
class MyStandardError < StandardError; end
class MySpecificError < MyStandardError; end
end
# app/services/filter/my_tested_service.rb
class Filter
class MyTestedService
def initialize
raise ::Filter::MySpecificError
end
end
end
I still don't know why it was working before..
You cannot define two constants at the same level in the same file. It is one constant, one file. This has not changed in Zeitwerk upgrades. You need one file for the standard error, and another file for the specific error.
I have been following various Ruby on rails REST API tutorials, specifically those using the GRAPE gem. On all these attempts, I have been encountering a undefined method `all' error. I am new to RoR and have been relying on the generators for model generation. Here is the error and code in question:
undefined method `all' for API::V1::EmailAuth:Class
module API
module V1
class EmailAuth < Grape::API
version 'v1' # path-based versioning by default
format :json # We don't like xml anymore
resource :email_auth do
desc "Return list of email_auth"
get do
EmailAuth.all # obviously you never want to call #all here
end
end
end
end
end
The file structure can be seen here: http://imgur.com/A2VnevL
(My rep is too low to post images)
This is my first time actually posting something in stackoverflow, do let me know if I can refine the question.
Just a note: I have refered to Undefined method `all' for ActiveSupport in Rails 4.0.0 app and various similar questions regarding namespace issues, but can seem to resolve this issue.
Thanks!
If you have a class Grape::API::EmailAuth and in this class you want to load the active record class EmailAuth, then you need to reference it by two colons:
get do
::EmailAuth.all
end
I'm trying to do somethinkg I've done hundered of times, and today it will not work... And I have no idea why.
I try to create an object with an attached file, and when object is created, do some treatment on the attached file. Or, the file is not present at the specified place !
I have an other model in the same application, doing Imagemagick treatments, and files are in the right directories. Here is no image treatment.
Here is some code
class Test
has_attached_file :file
after_create :do_some_stuff
def do_some_stuff
raise "File not found" if !File.exists?(self.file.path)
end
end
I obtain my File not found exception.
I tried to see if PostProcessing were executed, like this :
class Test
has_attached_file :file
after_post_process :print_log
after_create :print_created
def print_log
$stderr.puts "Processed."
end
def print_created
$stderr.puts "Created."
end
end
The "Processed" is correctly printed before the after_create method...
Do you have any idea ?
My configuration :
Rails 2.3.18
Ruby 1.8.6
Rspec 1.3.2
Paperclip 2.3.0
OK, my fault.
I read the code, and Paperclip call the save method with an after_save. And after_save is called after after_create.
Using Rails 3.2.2 and Ruby 1.9.2.
I have a rails mountable engine EngineA that declares a User class inheriting form ActiveRecord::Base. I have another engine EngineB that wants to inject functionality into EngineA::User. Right now what I have done is shown below:
Method 1:
#EngineA app/models/engine_a/user.rb
module EngineA
class User < ActiveRecord::Base
has_attached_file :avatar
has_many :somethings
end
end
#EngineB lib/engine_b/user.rb
module EngineB
module User
def self.extended obj
obj.class_eval do
has_many :something_elses
end
end
end
end
EngineA::User.extend EngineB::User
This gives me an uninitialized constant EngineA::User error. Even when I require that specific file I run into the problem of EngineA needing paperclip so that has_attached_file is understood. That road ended when I realized I would have to know and require the dependencies for EngineA inside EngineB.
Method 2:
I used the same code as before except I removed the last line EngineA::User.extend EngineB::User from the EngineB user.rb file. I then moved that call to an initializer inside EngineB.
#EngineB config/initializers/my_mixin.rb
EngineA::User.extend EngineB::User
This worked perfectly!!! Except in development mode when I would change code and the models would refresh. The only thing that was refreshed was the EngineA::User and not the mixin that I had put as an initializer. So once I changed code, I lost all of my extended functionality.
I'm not even positive this is the most 'efficient' way to do this... any help would be greatly appreciated. Thanks in advance.
According to the configuration documentation, you can use an ActionDispatch callback to load items. These callbacks will run when at every request if cache_classes is set to false, like in development mode.
Inside of your EngineB.rb file, you might try something like this:
if Rails.env.development?
ActionDispatch::Callbacks.to_prepare do
load "#{File.expand_path(File.dirname(__FILE__))}/../config/initializers/my_mixin.rb"
end
end
I'm using Rails 3, Uploadify, to send images to S3.
Right now all the images being upload have the MIME: application/octet-stream
I'd like to fix that but I'm getting the following error:
NoMethodError (undefined method `original_filename' for #<ActiveSupport::HashWithIndifferentAccess:0x107c81998>):
app/models/photo.rb:29:in `upload_file='
app/controllers/photos_controller.rb:15:in `upload'
app/middleware/flash_session_cookie_middleware.rb:14:in `call'
I think this is because all the tutorials out there aren't Rails 3 friendly. Anyone have any ideas? Here's the code:
# Controller
def create
#photo = Photo.new(:upload_file => params[:photo][:image])
...
end
# Model
class Photo < ActiveRecord::Base
require 'mime/types'
...
def upload_file=(data)
data.content_type = MIME::Types.type_for(data.original_filename).to_s
self.image = data
end
end
I'm not familiar with Uploadify, but it seems to be just a javascript generator...
You're passing a params value in as 'data' for #upload_file= . Then you're calling a method (#original_filename) on params[:photo][:image]. Rails is saying that params[:photo][:image] doesn't have such a method.
Is there some kind of File class in 'mime/types'? Should you be creating that File object first?
file = File.new(params[:photo][:image])
and then change that files attribute:
file.content_type = ...
EDIT:
Are you using the paperclip gem? The tutorial that you are using is using paperclip. So in "#asset.file_content_type = MIME::Types.type_for(#asset.original_filename).to_s", I think #asset is an instance of paperclip's File class which does have a #original_filename method. However, I don't see a #file_content_type=() method in the paperclip docs.