Forum: Ruby on Rails Newbie Question : else if

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
akira (Guest)
on 2009-02-08 07:00
(Received via mailing list)
Hello, i have started using ruby on rails, and searched the web for
this information but found nothing!
My problem is : i have main.html.rb and inside a conditional :

<% if controller.controller_name == "main" %>

  do something

<% else if controller.controller_name == "other_controller" %>

  do something

<% else %>

  do something

<% end %>

The problem is: im using netbeans and netbeans shows "Unexpected-end-
of-file" at the end in the close html tag.
So whats the problem? There is not else if tags in ror? If i use if -
else - end it runs fine but not if-else if-else-end.TIA
Simon M. (Guest)
on 2009-02-08 07:05
(Received via mailing list)
try elsif

Simon
Julian L. (Guest)
on 2009-02-08 15:35
(Received via mailing list)
Colin L. (Guest)
on 2009-02-08 15:56
(Received via mailing list)
2009/2/8 Julian L. <removed_email_address@domain.invalid>

>
> Elsif
>

elsif is the way to do it, but why doesn't 'else if' work? The 'if'
being
the statement to be executed in the 'else' case
wilson ikeda (Guest)
on 2009-02-08 20:05
(Received via mailing list)
Hi, thanks for the reply, the problem is : when i use elseif and run
the program, i get an error saying that it cannot recognize elseif and
strangely all ruby code in netbeans color syntax is in blue but not
elseif statements?. This is what i get when running elseif :

undefined method `elseif' for #<ActionView::Base:0x21dae8c>

Well, im using if (many_args) - end, if (many_args) - end for now.
Thanks for replying.
Colin L. (Guest)
on 2009-02-08 20:31
(Received via mailing list)
There is only one e in elsif (not elseif).  Don't ask me why, one of the
few
annoying things in Ruby.

2009/2/8 wilson ikeda <removed_email_address@domain.invalid>
Frederick C. (Guest)
on 2009-02-08 22:11
(Received via mailing list)
On Feb 8, 1:55 pm, Colin L. <removed_email_address@domain.invalid> wrote:
> 2009/2/8 Julian L. <removed_email_address@domain.invalid>
>
>
>
> > Elsif
>
> elsif is the way to do it, but why doesn't 'else if' work? The 'if' being
> the statement to be executed in the 'else' case
>

it does work, but

if foo
...
else if bla
...
end

isn't valid (since the else has no matching end statement)
You'd need
if foo
...
else
  if bla
    ...
  end
end


Fred
Julian L. (Guest)
on 2009-02-09 06:13
(Received via mailing list)
Not elseif
Elsif

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/
Julian L. (Guest)
on 2009-02-09 06:20
(Received via mailing list)
Hi,

All of the following are valid.
The third one is retarded, because by not putting the second if on a
new line, you don't get the indentation, so you can't visually tell if
you've got the right number of clauses. I prefer the fourth way.

Notice that the 3rd and 4th methods have an extra end, whereas the
second method has only one end.

if(true)
  puts 'yay'
else
  puts 'nay'
end

if(false)
  puts 'yay'
elsif(true)
  puts 'nay'
end

if(false)
  puts 'woo'
else if(true)
  puts 'boo'
end
end

if(false)
  puts 'woo'
else
  if(true)
    puts 'boo'
  end
end
This topic is locked and can not be replied to.