Case statements in views...strange error on OSX


#1

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” -%>

worked accounts

<% else -%>

worked else

<% end -%>

I have reset the server everything. I’m using rails 1.1.1 and the latest
stable ruby build. Please help?


#2

James W. 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” -%>

worked accounts

<% else -%>

worked else

<% 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?


#3

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


#4

James W. wrote:

Stephen B. 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 ??


#5

Stephen B. 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 ?


#6

James W. wrote:

James W. wrote:

Stephen B. 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 ?


#7

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 W. 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
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

_Kevin


#8

Kevin O. 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 W. 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
removed_email_address@domain.invalid
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” -%>

worked accounts

<% else -%>

worked else

<% 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”

worked accounts


else

worked else


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?


#9

On 4/25/06, Guest removed_email_address@domain.invalid 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

<% else %>
Not Book

<% end %>

and it worked just fine.

jt


#10

On 4/25/06, Guest removed_email_address@domain.invalid 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


#11

John T. wrote:

On 4/25/06, Guest removed_email_address@domain.invalid 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

<% else %>
Not Book

<% 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.


#12

John T. wrote:

On 4/25/06, Guest removed_email_address@domain.invalid 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:

Runme#index

Find me in app/views/runme/index.rhtml

<% case controller_controller_name %> <% when 'runme' %>

hello

<% else %>

not ok

<% 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:

Runme#index


2:

Find me in app/views/runme/index.rhtml


3: <% case controller_controller_name %>
4: <% when ‘runme’ %>
5:

hello


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:inrender_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:inrender_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:inrender_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:inmeasure’
/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:inperform_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:inperform_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:inperform_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:inprocess_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:inprocess’
/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:inhandle_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:inservice’
/usr/local/lib/ruby/1.8/webrick/httpserver.rb:65:in run' /usr/local/lib/ruby/1.8/webrick/server.rb:173:instart_thread’
/usr/local/lib/ruby/1.8/webrick/server.rb:162:in start_thread' /usr/local/lib/ruby/1.8/webrick/server.rb:95:instart’
/usr/local/lib/ruby/1.8/webrick/server.rb:92:in start' /usr/local/lib/ruby/1.8/webrick/server.rb:23:instart’
/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:indispatch’
/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:inrequire’
/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:incompile_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:inrender_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:inrender_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:inrender’
/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:inrender’
/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:inperform_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:inmeasure’
/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:inperform_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:inprocess_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:indispatch’
/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:inservice’
/usr/local/lib/ruby/1.8/webrick/httpserver.rb:104:in service' /usr/local/lib/ruby/1.8/webrick/httpserver.rb:65:inrun’
/usr/local/lib/ruby/1.8/webrick/server.rb:173:in start_thread' /usr/local/lib/ruby/1.8/webrick/server.rb:162:instart_thread’
/usr/local/lib/ruby/1.8/webrick/server.rb:95:in start' /usr/local/lib/ruby/1.8/webrick/server.rb:92:instart’
/usr/local/lib/ruby/1.8/webrick/server.rb:23:in start' /usr/local/lib/ruby/1.8/webrick/server.rb:82:instart’
/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:inrequire’
/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:inrequire’
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?


#13

On 4/25/06, Guest removed_email_address@domain.invalid wrote:

Runme#index

Find me in app/views/runme/index.rhtml

<% case controller_controller_name %> <% when 'runme' %>

hello

<% else %>

not ok

<% end %>

Was that just a typeo? -

case controller.controller_name


#14

What happens when you say:

<% case ‘runme’: %> instead of <% case ‘runme’ %>
?


#15

Wilson B. 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…


#16

On 4/25/06, Guest removed_email_address@domain.invalid 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

<% else %>
Not Book

<% end %>

jt


#17

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 %>