# If else statement problem

**URL:** <https://www.ruby-forum.com/t/if-else-statement-problem/119364>\
**Category:** Rails\
**Created:** [November 6, 2007, 12:25pm UTC](https://www.ruby-forum.com/t/if-else-statement-problem/119364 "2007-11-06T12:25:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [November 6, 2007, 12:25pm UTC](https://www.ruby-forum.com/t/if-else-statement-problem/119364/1 "2007-11-06T12:25:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [November 6, 2007, 12:31pm UTC](https://www.ruby-forum.com/t/if-else-statement-problem/119364/2 "2007-11-06T12:31:24Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [November 6, 2007, 12:46pm UTC](https://www.ruby-forum.com/t/if-else-statement-problem/119364/3 "2007-11-06T12:46:04Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [November 6, 2007, 12:46pm UTC](https://www.ruby-forum.com/t/if-else-statement-problem/119364/4 "2007-11-06T12:46:57Z")

</div>

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](http://www.imagemagick.org)  
Built for ruby 1.8.5 (2006-08-25) [i486-linux]  
Web page: [http://rmagick.rubyforge.org](http://rmagick.rubyforge.org)  
Email: removed\_email\_address@domain.invalid

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [November 6, 2007, 12:55pm UTC](https://www.ruby-forum.com/t/if-else-statement-problem/119364/5 "2007-11-06T12:55:09Z")

</div>

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
