I''m refactoring some code in order to conform to "the right
way" of doing
things. That is, I''m moving my image-saving code from my controller to
my
model. What worked fine in the controller class is not working in the model
class.
Currently, my code looks somewhat like this:
##############
class RegistrantsController < ApplicationController
def upload_image
@registrant = Registrant.find(session[:registrant_id])
if request.post?
if(params[:crop_params])
@registrant.save_image(params[:image_to_crop], params[:crop_params])
redirect_to :action => :index
else
@registrant.image = params[:registrant][:image]
File.chmod(0644, @registrant.image)
end
end
end
end
class Registrant < ActiveRecord::Base
require_gem ''rmagick''
def save_image(image_to_crop, crop_params)
img_base = RAILS_ROOT + "/public/images/registrants/"
img_file = self.id.to_s + ".jpg"
y, x, w, h = crop_params.split('','')
img = Magick::ImageList.new(image_to_crop) # this is throwing
"uninitialized constant ImageList"
img.crop!(x.to_i, y.to_i, w.to_i, h.to_i)
img.resize!(120, 120)
color_thumb = img.resize(32, 32)
grey_img = img.quantize(256, Magick::GRAYColorspace)
img.write(img_path + img_filename)
grey_img.write(img_base + ''greyscale/'' + img_filename)
color_thumb.write(img_base + ''thumbnail/'' + img_filename)
end
end
##############
Note the comment in there (# this is throwing "uninitialized constant
ImageList")
Can anyone see what I''ve done wrong, or otherwise help out?
Thanks,
David Rose
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails
Move the ''require_gem "rmagick"'' line outside the
class definition for
Registrant
require_gem ''rmagick''
class Registrant < ActiveRecord::Base
...
________________________________
From: David Rose [mailto:doppler-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org]
Sent: Monday, December 12, 2005 10:04 AM
To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: [Rails] Problem using rmagick in model class
I''m refactoring some code in order to conform to "the right
way" of
doing things. That is, I''m moving my image-saving code from my
controller to my model. What worked fine in the controller class is not
working in the model class.
Currently, my code looks somewhat like this:
##############
class RegistrantsController < ApplicationController
def upload_image
@registrant = Registrant.find(session[:registrant_id])
if request.post?
if(params[:crop_params])
@registrant.save_image(params[:image_to_crop],
params[:crop_params])
redirect_to :action => :index
else
@registrant.image = params[:registrant][:image]
File.chmod(0644, @registrant.image)
end
end
end
end
class Registrant < ActiveRecord::Base
require_gem ''rmagick''
def save_image(image_to_crop, crop_params)
img_base = RAILS_ROOT + "/public/images/registrants/"
img_file = self.id.to_s + ".jpg"
y, x, w, h = crop_params.split('','')
img = Magick::ImageList.new(image_to_crop) # this is throwing
"uninitialized constant ImageList"
img.crop!(x.to_i, y.to_i, w.to_i, h.to_i)
img.resize!(120, 120)
color_thumb = img.resize(32, 32)
grey_img = img.quantize(256, Magick::GRAYColorspace)
img.write(img_path + img_filename)
grey_img.write(img_base + ''greyscale/'' + img_filename)
color_thumb.write(img_base + ''thumbnail/'' + img_filename)
end
end
##############
Note the comment in there (# this is throwing "uninitialized constant
ImageList")
Can anyone see what I''ve done wrong, or otherwise help out?
Thanks,
David Rose
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails
Doh. So, it might be helpful if I understood why my previous code, where all of this was in the controller, worked fine, even when the require_gem statement was called within a class method. The controller method looked like this: def upload_image(*some_args) require_gem 'rmagick' #do stuff with Magick::ImageList end On 12/12/05, Tom Fakes <Tom@tomandlisa.us> wrote:> > Move the 'require_gem "rmagick"' line outside the class definition for > Registrant > > > > require_gem 'rmagick' > class Registrant < ActiveRecord::Base > … > > > > > ------------------------------ > > *From:* David Rose [mailto:doppler@gmail.com] > *Sent:* Monday, December 12, 2005 10:04 AM > *To:* rails@lists.rubyonrails.org > *Subject:* [Rails] Problem using rmagick in model class > > > > I'm refactoring some code in order to conform to "the right way" of doing > things. That is, I'm moving my image-saving code from my controller to my > model. What worked fine in the controller class is not working in the model > class. > > Currently, my code looks somewhat like this: > ############## > class RegistrantsController < ApplicationController > def upload_image > @registrant = Registrant.find(session[:registrant_id]) > if request.post? > if(params[:crop_params]) > @registrant.save_image(params[:image_to_crop], > params[:crop_params]) > redirect_to :action => :index > else > @registrant.image = params[:registrant][:image] > File.chmod(0644, @registrant.image) > end > end > end > end > > class Registrant < ActiveRecord::Base > require_gem 'rmagick' > > def save_image(image_to_crop, crop_params) > > img_base = RAILS_ROOT + "/public/images/registrants/" > img_file = self.id.to_s + ".jpg" > y, x, w, h = crop_params.split(',') > > img = Magick::ImageList.new(image_to_crop) # this is throwing > "uninitialized constant ImageList" > > img.crop!(x.to_i, y.to_i, w.to_i, h.to_i) > img.resize!(120, 120) > color_thumb = img.resize(32, 32) > grey_img = img.quantize(256, Magick::GRAYColorspace) > img.write(img_path + img_filename) > grey_img.write(img_base + 'greyscale/' + img_filename) > color_thumb.write(img_base + 'thumbnail/' + img_filename) > end > end > ############## > > Note the comment in there (# this is throwing "uninitialized constant > ImageList") > > Can anyone see what I've done wrong, or otherwise help out? > > Thanks, > David Rose > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- David Rose Webmaster, SXSW Conferences and Festivals SXSW 2005 - March 11-20, 2005 - Austin, Texas http://sxsw.com/ _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
I think that in the code that didn''t work, you were, in effect,
creating
a class called Registrant::Magick::ImageList, because the *require* was
inside the class definition (this is how mixins do their magic).
However, it seems that doesn''t apply if you use *require* inside a
method inside a class - your controller code that works.
Either put the *require* outside the class or module, or inside a method
to get the behavior you want.
________________________________
From: David Rose [mailto:doppler-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org]
Sent: Monday, December 12, 2005 9:00 PM
To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: Re: [Rails] Problem using rmagick in model class
Doh.
So, it might be helpful if I understood why my previous code, where all
of this was in the controller, worked fine, even when the require_gem
statement was called within a class method. The controller method looked
like this:
def upload_image(*some_args)
require_gem ''rmagick''
#do stuff with Magick::ImageList
end
On 12/12/05, Tom Fakes < Tom-SpgrXpdGZSiNACpsiTyfug@public.gmane.org
<mailto:Tom-SpgrXpdGZSiNACpsiTyfug@public.gmane.org> >
wrote:
Move the ''require_gem "rmagick"'' line outside the
class definition for
Registrant
require_gem ''rmagick''
class Registrant < ActiveRecord::Base
...
________________________________
From: David Rose [mailto:doppler-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org]
Sent: Monday, December 12, 2005 10:04 AM
To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: [Rails] Problem using rmagick in model class
I''m refactoring some code in order to conform to "the right
way" of
doing things. That is, I''m moving my image-saving code from my
controller to my model. What worked fine in the controller class is not
working in the model class.
Currently, my code looks somewhat like this:
##############
class RegistrantsController < ApplicationController
def upload_image
@registrant = Registrant.find(session[:registrant_id])
if request.post?
if(params[:crop_params])
@registrant.save_image(params[:image_to_crop],
params[:crop_params])
redirect_to :action => :index
else
@registrant.image = params[:registrant][:image]
File.chmod(0644, @registrant.image)
end
end
end
end
class Registrant < ActiveRecord::Base
require_gem ''rmagick''
def save_image(image_to_crop, crop_params)
img_base = RAILS_ROOT + "/public/images/registrants/"
img_file = self.id.to_s + ".jpg"
y, x, w, h = crop_params.split('','')
img = Magick::ImageList.new(image_to_crop) # this is throwing
"uninitialized constant ImageList"
img.crop!(x.to_i, y.to_i, w.to_i, h.to_i)
img.resize!(120, 120)
color_thumb = img.resize(32, 32)
grey_img = img.quantize(256, Magick::GRAYColorspace)
img.write(img_path + img_filename)
grey_img.write(img_base + ''greyscale/'' + img_filename)
color_thumb.write(img_base + ''thumbnail/'' + img_filename)
end
end
##############
Note the comment in there (# this is throwing "uninitialized constant
ImageList")
Can anyone see what I''ve done wrong, or otherwise help out?
Thanks,
David Rose
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails
--
David Rose
Webmaster, SXSW Conferences and Festivals
SXSW 2005 - March 11-20, 2005 - Austin, Texas
http://sxsw.com/
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails
ara.t.howard-32lpuo7BZBA@public.gmane.org
2005-Dec-13 07:06 UTC
RE: Problem using rmagick in model class
On Mon, 12 Dec 2005, Tom Fakes wrote:> I think that in the code that didn''t work, you were, in effect, creating a > class called Registrant::Magick::ImageList, because the *require* was inside > the class definition (this is how mixins do their magic). However, it seems > that doesn''t apply if you use *require* inside a method inside a class - > your controller code that works. > > Either put the *require* outside the class or module, or inside a method > to get the behavior you want.''require'' cares not where it''s called from. try prefacing the ''Magick'' constant with ''::'' to make sure you are refering to the top-level Magick module. in general you should always do this since: harp:~ > cat a.rb class Foo; end class Bar p Foo # gets top level foo end class FooBar Foo = 42 p Foo p ::Foo # gets top level foo end harp:~ > ruby a.rb Foo 42 Foo rails does some voodoo with const_missing that may have caused your referencing it to define one in your model - or perhaps you have another Magick const defined somewhere else in your code, since the code is your model now this should be quite easy to test. regards. -a -- ==============================================================================| ara [dot] t [dot] howard [at] noaa [dot] gov | all happiness comes from the desire for others to be happy. all misery | comes from the desire for oneself to be happy. | -- bodhicaryavatara ============================================================================== _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Ara, your suggestion to preface the call with :: worked perfectly. I''m gonna stab in the dark and assume my use of file_column elsewhere in the model may be to blame. Thanks David Rose On 12/13/05, ara.t.howard-32lpuo7BZBA@public.gmane.org <ara.t.howard-32lpuo7BZBA@public.gmane.org> wrote:> > On Mon, 12 Dec 2005, Tom Fakes wrote: > > > I think that in the code that didn''t work, you were, in effect, creating > a > > class called Registrant::Magick::ImageList, because the *require* was > inside > > the class definition (this is how mixins do their magic). However, it > seems > > that doesn''t apply if you use *require* inside a method inside a class - > > your controller code that works. > > > > Either put the *require* outside the class or module, or inside a method > > to get the behavior you want. > > ''require'' cares not where it''s called from. try prefacing the ''Magick'' > constant with ''::'' to make sure you are refering to the top-level Magick > module. in general you should always do this since: > > harp:~ > cat a.rb > class Foo; end > > class Bar > p Foo # gets top level foo > end > > class FooBar > Foo = 42 > p Foo > p ::Foo # gets top level foo > end > > > harp:~ > ruby a.rb > Foo > 42 > Foo > > rails does some voodoo with const_missing that may have caused your > referencing > it to define one in your model - or perhaps you have another Magick const > defined somewhere else in your code, since the code is your model now this > should be quite easy to test. > > regards. > > -a > -- > > ==============================================================================> | ara [dot] t [dot] howard [at] noaa [dot] gov > | all happiness comes from the desire for others to be happy. all misery > | comes from the desire for oneself to be happy. > | -- bodhicaryavatara > > ==============================================================================> > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- David Rose Webmaster, SXSW Conferences and Festivals SXSW 2005 - March 11-20, 2005 - Austin, Texas http://sxsw.com/ _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails