If else statement problem

Hey,

I’m trying to execute the code below which i have tested and works fine:

<–text.rb–>

p = 2

if p == 1
pos = NorthGravity
elsif p == 2
pos = SouthGravity
else
end

clown = Magick::ImageList.new(“3.jpeg”)
text = Magick::Draw.new
text.annotate(clown, 0, 0, 0, 0, “Text”) {
self.font_family = ‘Helvetica’
self.font_style = Magick::ItalicStyle
self.gravity = pos
self.pointsize = 20
self.stroke = ‘transparent’
self.fill = ‘#0000A9
self.font_weight = Magick::BolderWeight
}
clown.write(‘text.jpg’)

However when I try to do almost the same in my rails application I get
an error:

TypeError in MainController#addtext

wrong enumeration type - expected Magick::GravityType, got NilClass

Below is the code:

<–main_controller.rb–>

def addtext
@logo = Logo.find(params[:id])
t = params[:text]
c = params[:colour]
s = params[:size].to_f
p = params[:position]

if p == 1
pos = NorthGravity
elsif p == 2
pos = SouthGravity
else
end

@image = File.join(RAILS_ROOT, “public/images/logo.jpg”)
@image = Magick::ImageList.new(@image)
@image = @image.first

text = Magick::Draw.new

text.annotate(@image, 0, 0, 0, 0, t){
self.font_family = ‘Helvetica’
self.font_style = Magick::ItalicStyle
self.gravity = pos
self.pointsize = s
self.stroke = ‘transparent’
self.fill = c
self.font_weight = Magick::BolderWeight
}
@image = @image.write(File.join(RAILS_ROOT, “public/images/logo.jpg”))
{self.quality = 100}

render :action => “edit”

end

p will be either 1 or 2 depending on the value of params[:position] from
the form.

Select a position ----------------------
    <option value="1">North</option>

    <option value="2">South</option>

Any help??

Cheers

Perhaps check to see if your version of ImageMagick matches your rmagick
gem. I had this problem a few days ago, updating to a newer ImageMagick
solved the problem. There is also another fix, where it says it got the
wrong type (it’ll be in an rmagick file, I have it as a plugin under my
rails directory), open the file at that line and at the end of the
statement
(it will be a function followed by several params), remove the last
param
(it’s a true from memory).

I’m doing all this from memory, so sorry if it doesn’t match up, but
that
also solved the problem for me (and took me quite a while to find :().

Good luck,
James

On Nov 6, 2007 9:55 PM, Rajeev K. [email protected]

p will be either 1 or 2 depending on the value of params[:position]
from
the form.

actually params[:position] will be the string ‘1’ or the string ‘2’,
however you’re comparing it to integers, hence you fall through to the
else statement.

fred

Hey,

These are the versions:

irb(main):012:0> puts Magick::Long_version
This is RMagick 1.15.10 ($Date: 2007/06/09 16:45:25 $) Copyright (C)
2007 by Timothy P. Hunter
Built with ImageMagick 6.3.6 10/15/07 Q16 http://www.imagemagick.org
Built for ruby 1.8.5 (2006-08-25) [i486-linux]
Web page: http://rmagick.rubyforge.org
Email: [email protected]

How do I locate the rmagick file you mentioned, because I don’t have it
as a plugin under my rails directory?

Cheers

James Brooks wrote:

Perhaps check to see if your version of ImageMagick matches your rmagick
gem. I had this problem a few days ago, updating to a newer ImageMagick
solved the problem. There is also another fix, where it says it got the
wrong type (it’ll be in an rmagick file, I have it as a plugin under my
rails directory), open the file at that line and at the end of the
statement
(it will be a function followed by several params), remove the last
param
(it’s a true from memory).

I’m doing all this from memory, so sorry if it doesn’t match up, but
that
also solved the problem for me (and took me quite a while to find :().

Good luck,
James

On Nov 6, 2007 9:55 PM, Rajeev K. [email protected]

Thanks Fred,

That worked. Stupid mistake by me really just have to convert to
integer.

Cheers

Frederick C. wrote:

p will be either 1 or 2 depending on the value of params[:position]
from
the form.

actually params[:position] will be the string ‘1’ or the string ‘2’,
however you’re comparing it to integers, hence you fall through to the
else statement.

fred