This project is archived and is in readonly mode.

#3933 ✓invalid
Camilo Lopez

Application will die with syntax error when trying to define hyphenated resources

Reported by Camilo Lopez | February 11th, 2010 @ 05:04 PM

This resource declaration:

   resources user do 
     resource 'my-nested-resource'

will end up in a syntax error when trying to boot the application:

/usr/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta/lib/action_dispatch/routing/route_set.rb:151:in `define_hash_access': compile error (SyntaxError)
/usr/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta/lib/action_dispatch/routing/route_set.rb:149: syntax error, unexpected '-', expecting '\n' or ';'
...ef hash_for_two_oh_user_basic-stats_url(options = nil)      ...
/usr/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta/lib/action_dispatch/routing/route_set.rb:151: syntax error, unexpected kEND, expecting $end
              end                              ...

This happens in the generated routing code using module_eval, from actionpack/lib/action_dispatch/routing/route_set.rb

def define_hash_access(route, name, kind, options)
  selector = hash_access_name(name, kind)
  # We use module_eval to avoid leaks
  @module.module_eval <<-END_EVAL, __FILE__, __LINE__ + 1
    def #{selector}(options = nil) # def hash_for_users_url(options = nil)
    options ? #{options.inspect}.merge(options) : #{options.inspect} # options ? {:only_path=>false}.merge(options) :   {:only_path=>false}
end # end
    protected :#{selector} # protected :hash_for_users_url

That error can be avoided by using the define_method('method-name') instead of a plain def and would allow for hyphenated resources as well. However I'm not sure if there is a performance reason not do use define_method.

Comments and changes to this ticket

  • Carl

    Carl February 11th, 2010 @ 05:52 PM

    I'm also having the same problem, how can I define hyphened routes?

  • Carl

    Carl February 11th, 2010 @ 05:53 PM

    • Tag changed from resources hyphenated syntax error 3.0 beta to actionpack, hyphenatedroutes, router, routingerror
  • Lachlan Sylvester

    Lachlan Sylvester February 11th, 2010 @ 10:33 PM

    Carl, have you tried resource :my_resource, :as => 'my-resource' ?

    It would be nice is to could configure the router to generate routes with hyphens instead of underscores by default.

    Camilo, are you just wanting hyphens in the urls? I can't see a reason that you would want to to define _path and _url methods with hyphens in them.

  • Camilo Lopez

    Camilo Lopez February 11th, 2010 @ 10:45 PM


    I don't really need hyphenated urls, what is important are the resource names, apparently what you suggested to Carl will be the perfect solution : :as => 'my-resource'

  • David Trasbo

    David Trasbo April 14th, 2010 @ 08:19 PM

    • Assigned user set to “Ryan Bigg”

    As suggested above this is definitely not a bug. There is no sense in defining a method with a hyphen in its name as the only way to call it is by using send, which is obviously inconvenient. Use the :as option. This ticket can be closed.

  • Ryan Bigg

    Ryan Bigg April 15th, 2010 @ 11:01 AM

    • State changed from “new” to “invalid”
  • Brent Greeff

    Brent Greeff December 24th, 2010 @ 02:35 AM

    • Importance changed from “” to “Low”

    This is not working for me:

    None of my hypenated roots are working under Rails 3 anymore:

    resources :invitations, :as => 'send-invitation'


    /opt/ruby-enterprise-1.8.7-2010.02/lib/ruby/gems/1.8/gems/actionpack-3.0.3/lib/action_dispatch/routing/route_set.rb:163:in define_hash_access': compile error (SyntaxError)

    /opt/ruby-enterprise-1.8.7-2010.02/lib/ruby/gems/1.8/gems/actionpack-3.0.3/lib/action_dispatch/routing/route_set.rb:161: syntax error, unexpected '-', expecting '\n' or ';' ... def hash_for_send-invitation_index_path(options

    resources :invitations, :as => 'send_invitation'

    Does not do what I want either, new_invitation_path is undefined?
    and the urls have not changed.

  • Brent Greeff

    Brent Greeff December 25th, 2010 @ 01:50 AM

    OK, figured it out,

    resources 'send-invitation', :as => 'invitations', :controller => 'invitations'

    does the trick.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>

People watching this ticket