Forum: Ruby on Rails case statements in views...strange error on OSX

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.
B0be4bfef5a22697a0f44f4fe295aaef?d=identicon&s=25 James Whittaker (aftershock)
on 2006-04-25 15:35
I have a simple case statement in one of my views. This works fine on my
windows machine. Move to the mac and I get: "parse error, unexpected
tIDENTIFIER, expecting kWHEN".

I have not changed anything but cannot resolve the issue. my view has
this in it (simplified for test)

<% case controller.controller_name -%>
<% when "accounts" -%>
<h3>worked accounts</h3>
<% else -%>
<h3>worked else</h3>
<% end -%>

I have reset the server everything. I'm using rails 1.1.1 and the latest
stable ruby build. Please help?
B0be4bfef5a22697a0f44f4fe295aaef?d=identicon&s=25 James Whittaker (aftershock)
on 2006-04-25 15:48
James Whittaker wrote:
> I have a simple case statement in one of my views. This works fine on my
> windows machine. Move to the mac and I get: "parse error, unexpected
> tIDENTIFIER, expecting kWHEN".
>
> I have not changed anything but cannot resolve the issue. my view has
> this in it (simplified for test)
>
> <% case controller.controller_name -%>
> <% when "accounts" -%>
> <h3>worked accounts</h3>
> <% else -%>
> <h3>worked else</h3>
> <% end -%>
>
> I have reset the server everything. I'm using rails 1.1.1 and the latest
> stable ruby build. Please help?

Is it because I cannot split a case statement up like an if statement?
882cc23c77c5c6d27613c51396a02a0d?d=identicon&s=25 Stephen Bartholomew (Guest)
on 2006-04-25 15:58
(Received via mailing list)
I've tested this code on Freebsd 5.3/Ruby 1.8/rails 1.0 and it runs fine
so i wouldn't have thought it's syntax...

Steve
B0be4bfef5a22697a0f44f4fe295aaef?d=identicon&s=25 James Whittaker (aftershock)
on 2006-04-25 16:28
Stephen Bartholomew wrote:
> I've tested this code on Freebsd 5.3/Ruby 1.8/rails 1.0 and it runs fine
> so i wouldn't have thought it's syntax...
>
> Steve

Thanks Steve, still not working, really bugging me this is. Could
someone try on OSX to check ?
B0be4bfef5a22697a0f44f4fe295aaef?d=identicon&s=25 James Whittaker (aftershock)
on 2006-04-25 16:48
James Whittaker wrote:
> Stephen Bartholomew wrote:
>> I've tested this code on Freebsd 5.3/Ruby 1.8/rails 1.0 and it runs fine
>> so i wouldn't have thought it's syntax...
>>
>> Steve
>
> Thanks Steve, still not working, really bugging me this is. Could
> someone try on OSX to check ?

Still no joy there seems to be a direct problem with using case
statements in rails on OSX. Everything I try results in an error. Is
this linked to rails 1.1.1 ??
E9f0b31b9e36e1cbecd73eb44c0d6e68?d=identicon&s=25 Guest (Guest)
on 2006-04-25 18:05
James Whittaker wrote:
> James Whittaker wrote:
>> Stephen Bartholomew wrote:
>>> I've tested this code on Freebsd 5.3/Ruby 1.8/rails 1.0 and it runs fine
>>> so i wouldn't have thought it's syntax...
>>>
>>> Steve
>>
>> Thanks Steve, still not working, really bugging me this is. Could
>> someone try on OSX to check ?
>
> Still no joy there seems to be a direct problem with using case
> statements in rails on OSX. Everything I try results in an error. Is
> this linked to rails 1.1.1 ??

Has anyone used a case statement in a view without issue on OSX ?
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-04-25 18:23
(Received via mailing list)
Have you been retyping this in OSX, or are you still working with the
same file from your PC?  Could it be a character set issue?

On Tuesday, April 25, 2006, at 4:48 PM, James Whittaker wrote:
>Still no joy there seems to be a direct problem with using case
>statements in rails on OSX. Everything I try results in an error. Is
>this linked to rails 1.1.1 ??
>
>--
>Posted via http://www.ruby-forum.com/.
>_______________________________________________
>Rails mailing list
>Rails@lists.rubyonrails.org
>http://lists.rubyonrails.org/mailman/listinfo/rails


_Kevin
E9f0b31b9e36e1cbecd73eb44c0d6e68?d=identicon&s=25 Guest (Guest)
on 2006-04-25 18:35
Kevin Olbrich wrote:
> Have you been retyping this in OSX, or are you still working with the
> same file from your PC?  Could it be a character set issue?
>
> On Tuesday, April 25, 2006, at 4:48 PM, James Whittaker wrote:
>>Still no joy there seems to be a direct problem with using case
>>statements in rails on OSX. Everything I try results in an error. Is
>>this linked to rails 1.1.1 ??
>>
>>--
>>Posted via http://www.ruby-forum.com/.
>>_______________________________________________
>>Rails mailing list
>>Rails@lists.rubyonrails.org
>>http://lists.rubyonrails.org/mailman/listinfo/rails
>
>
> _Kevin

I have tried both. What I can now gather is that when you use a case
statement inside a view it does not like being written like this:
<% case controller.controller_name -%>
<% when "accounts" -%>
<h3>worked accounts</h3>
<% else -%>
<h3>worked else</h3>
<% end -%>

If I retype it like the example underneath it does not error but also
does not work, produces nothing. Yet on the PC it works fine.

<% case controller.controller_name
when "accounts"
'<h3>worked accounts</h3>'
else
'<h3>worked else</h3>'
end %>

What makes it even more interesting is that I wish to call a helper
method inside the case statement like this:

<% case controller.controller_name -%>

<% when "accounts" -%>

    <%= sub_tab "Overview", :controller=>'accounts', :action =>
'index'%>
    <%= sub_tab "List Employees", :controller=>'accounts', :action =>
'list' %>
    <%= sub_tab "Roles & Permissions", :controller=>'accounts', :action
=> 'permissions' %>

<% when "clients" -%>

    <%= sub_tab "List Clients", :controller=>'clients', :action =>
'index' %>

<% else -%>

    <%= sub_tab "Overview", :controller=>'projects', :action => 'index'
%>

<% end -%>

Is there is another way to do what i'm trying to do without using case
statements?
B07c187ac858535358faa9365b66e657?d=identicon&s=25 John Tsombakos (Guest)
on 2006-04-25 18:48
(Received via mailing list)
On 4/25/06, Guest <j.whittaker@spectrais.com> wrote:
> Has anyone used a case statement in a view without issue on OSX ?

I just tried a test, using OS X (10.4.6, Rails 1.1.2):

<% case @book.name -%>
<% when "book" %>
Is Book<br>
<% else %>
Not Book<br>
<% end %>


and it worked just fine.

jt
E9f0b31b9e36e1cbecd73eb44c0d6e68?d=identicon&s=25 Guest (Guest)
on 2006-04-25 18:53
John Tsombakos wrote:
> On 4/25/06, Guest <j.whittaker@spectrais.com> wrote:
>> Has anyone used a case statement in a view without issue on OSX ?
>
> I just tried a test, using OS X (10.4.6, Rails 1.1.2):
>
> <% case @book.name -%>
> <% when "book" %>
> Is Book<br>
> <% else %>
> Not Book<br>
> <% end %>
>
>
> and it worked just fine.
>
> jt

Thanks jt. So I have a ruby or rails issue then. i am running the same
as you so that is really odd. here goes for an update.
B07c187ac858535358faa9365b66e657?d=identicon&s=25 John Tsombakos (Guest)
on 2006-04-25 19:07
(Received via mailing list)
On 4/25/06, Guest <j.whittaker@spectrais.com> wrote:

> Thanks jt. So I have a ruby or rails issue then. i am running the same
> as you so that is really odd. here goes for an update.

That is an odd one. Can you do a case statement using irb?

irb(main):001:0> b='book'
=> "book"
irb(main):002:0> case b
irb(main):003:1> when 'car'
irb(main):004:1> puts "is a car"
irb(main):005:1> when 'book'
irb(main):006:1> puts "is a book"
irb(main):007:1> else
irb(main):008:1* puts "nothing"
irb(main):009:1> end
is a book
E9f0b31b9e36e1cbecd73eb44c0d6e68?d=identicon&s=25 Guest (Guest)
on 2006-04-25 19:14
John Tsombakos wrote:
> On 4/25/06, Guest <j.whittaker@spectrais.com> wrote:
>
>> Thanks jt. So I have a ruby or rails issue then. i am running the same
>> as you so that is really odd. here goes for an update.
>
> That is an odd one. Can you do a case statement using irb?
>
> irb(main):001:0> b='book'
> => "book"
> irb(main):002:0> case b
> irb(main):003:1> when 'car'
> irb(main):004:1> puts "is a car"
> irb(main):005:1> when 'book'
> irb(main):006:1> puts "is a book"
> irb(main):007:1> else
> irb(main):008:1* puts "nothing"
> irb(main):009:1> end
> is a book

That works fine. I have just updated ruby and rails to the latest
versions and it still does not work. I have created a new rails project
and just added this in the view:

<h1>Runme#index</h1>
<p>Find me in app/views/runme/index.rhtml</p>
<% case controller_controller_name %>
<% when 'runme' %>
<h3>hello</h3>
<% else %>
<h1>not ok</h1>
<% end %>

This is the error I get:

 SyntaxError in Runme#index

Showing app/views/runme/index.rhtml where line #3 raised:

compile error
script/../config/../app/views/runme/index.rhtml:3: parse error,
unexpected tIDENTIFIER, expecting kWHEN
 case controller_controller_name ; _erbout.concat "\n"
                                          ^
script/../config/../app/views/runme/index.rhtml:4: parse error,
unexpected kWHEN, expecting kEND
 when 'runme' ; _erbout.concat "\n"
     ^
script/../config/../app/views/runme/index.rhtml:10: parse error,
unexpected kEND, expecting $

Extracted source (around line #3):

1: <h1>Runme#index</h1>
2: <p>Find me in app/views/runme/index.rhtml</p>
3: <% case controller_controller_name %>
4: <% when 'runme' %>
5: <h3>hello</h3>
6: <% else %>

Trace of template inclusion: /app/views/runme/index.rhtml

RAILS_ROOT: script/../config/..
Application Trace | Framework Trace | Full Trace

#{RAILS_ROOT}/app/views/runme/index.rhtml:10:in `compile_template'

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:305:in
`compile_and_render_template'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:290:in
`render_template'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:249:in
`render_file'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:699:in
`render_file'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:621:in
`render_with_no_layout'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/layout.rb:251:in
`render_without_benchmark'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:53:in
`render'
/usr/local/lib/ruby/1.8/benchmark.rb:293:in `measure'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:53:in
`render'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:911:in
`perform_action_without_filters'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:368:in
`perform_action_without_benchmark'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in
`perform_action_without_rescue'
/usr/local/lib/ruby/1.8/benchmark.rb:293:in `measure'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in
`perform_action_without_rescue'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/rescue.rb:82:in
`perform_action'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:381:in
`process_without_filters'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:377:in
`process_without_session_management_support'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/session_management.rb:117:in
`process'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/dispatcher.rb:38:in
`dispatch'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick_server.rb:115:in
`handle_dispatch'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick_server.rb:81:in
`service'
/usr/local/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
/usr/local/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/usr/local/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
/usr/local/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
/usr/local/lib/ruby/1.8/webrick/server.rb:95:in `start'
/usr/local/lib/ruby/1.8/webrick/server.rb:92:in `start'
/usr/local/lib/ruby/1.8/webrick/server.rb:23:in `start'
/usr/local/lib/ruby/1.8/webrick/server.rb:82:in `start'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick_server.rb:67:in
`dispatch'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/commands/servers/webrick.rb:59
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in
`require'
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:in
`require'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/commands/server.rb:30
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`require'
script/server:3

#{RAILS_ROOT}/app/views/runme/index.rhtml:10:in `compile_template'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:305:in
`compile_and_render_template'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:290:in
`render_template'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:249:in
`render_file'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:699:in
`render_file'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:621:in
`render_with_no_layout'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/layout.rb:251:in
`render_without_benchmark'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:53:in
`render'
/usr/local/lib/ruby/1.8/benchmark.rb:293:in `measure'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:53:in
`render'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:911:in
`perform_action_without_filters'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:368:in
`perform_action_without_benchmark'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in
`perform_action_without_rescue'
/usr/local/lib/ruby/1.8/benchmark.rb:293:in `measure'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in
`perform_action_without_rescue'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/rescue.rb:82:in
`perform_action'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:381:in
`process_without_filters'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:377:in
`process_without_session_management_support'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/session_management.rb:117:in
`process'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/dispatcher.rb:38:in
`dispatch'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick_server.rb:115:in
`handle_dispatch'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick_server.rb:81:in
`service'
/usr/local/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
/usr/local/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/usr/local/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
/usr/local/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
/usr/local/lib/ruby/1.8/webrick/server.rb:95:in `start'
/usr/local/lib/ruby/1.8/webrick/server.rb:92:in `start'
/usr/local/lib/ruby/1.8/webrick/server.rb:23:in `start'
/usr/local/lib/ruby/1.8/webrick/server.rb:82:in `start'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick_server.rb:67:in
`dispatch'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/commands/servers/webrick.rb:59
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in
`require'
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:in
`require'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/commands/server.rb:30
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`require'
script/server:3

Request

Parameters: None

Show session dump

---
flash: !map:ActionController::Flash::FlashHash {}


Response
Headers: {"cookie"=>[], "Cache-Control"=>"no-cache"}


I dont know what to do?
B07c187ac858535358faa9365b66e657?d=identicon&s=25 John Tsombakos (Guest)
on 2006-04-25 19:22
(Received via mailing list)
On 4/25/06, Guest <j.whittaker@spectrais.com> wrote:
> <h1>Runme#index</h1>
> <p>Find me in app/views/runme/index.rhtml</p>
> <% case controller_controller_name %>
> <% when 'runme' %>
> <h3>hello</h3>
> <% else %>
> <h1>not ok</h1>
> <% end %>

Was that just a typeo? -
...
  case controller.controller_name
...
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-04-25 19:26
(Received via mailing list)
What happens when you say:

<% case 'runme': %> instead of <% case 'runme' %>
?
E9f0b31b9e36e1cbecd73eb44c0d6e68?d=identicon&s=25 Guest (Guest)
on 2006-04-25 19:45
Wilson Bilkovich wrote:
> What happens when you say:
>
> <% case 'runme': %> instead of <% case 'runme' %>
> ?

SOLVED !!!

It seems that you have to have the case and the first when on the same
ruby line e.g:

<% case controller.controller_name when 'runme' %>

instead of:


<% case controller.controller_name %>
<% when 'runme' %>

How strange is that, I thought that it would not matter? Anyway on with
the project...
B2317e45c120687eb9ba6140c3a3e5e2?d=identicon&s=25 Brad C. (greenbox)
on 2006-04-25 19:48
I had this same problem yesterday. I'm on XP though, Ruby 1.8.4 mswin32,
and Rails 1.1.2. I found two ways around it:

1. Encapsulate the whole case statement in <%= %>
2. Encapsulate the first when with the case keyword, i.e.

<% b = :banana %>

<% case b
when String %>

It's a string!

<% when Symbol %>

No it's a symbol!

<% end %>
B07c187ac858535358faa9365b66e657?d=identicon&s=25 John Tsombakos (Guest)
on 2006-04-25 20:19
(Received via mailing list)
On 4/25/06, Guest <j.whittaker@spectrais.com> wrote:
>
> <% case controller.controller_name when 'runme' %>
>
> instead of:
>
>
> <% case controller.controller_name %>
> <% when 'runme' %>
>
> How strange is that, I thought that it would not matter? Anyway on with
> the project...

Very odd.

As I posted above, this code works just fine in a test app in
show.rhtml:

<% case controller.controller_name -%>
<% when "books" %>
Is Book<br>
<% else %>
Not Book<br>
<% end %>

jt
This topic is locked and can not be replied to.