Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

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

#3933

This resource declaration:

   resources user do 
     resource 'my-nested-resource'
   end

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
   END_EVAL

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.

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

State: invalid
Milestone: none
Assigned to: Ryan Bigg Ryan Bigg
Importance: Low

Activity

  1. Carl
    Carl

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

    February 11th, 2010 @ 05:52 PM

  2. Carl
    Carl
    • Tag changed from resources hyphenated syntax error 3.0 beta to actionpack, hyphenatedroutes, router, routingerror

    February 11th, 2010 @ 05:53 PM

  3. Lachlan Sylvester
    Lachlan Sylvester

    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.

    February 11th, 2010 @ 10:33 PM

  4. Camilo Lopez
    Camilo Lopez

    Lachlan,

    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'

    February 11th, 2010 @ 10:45 PM

  5. David Trasbo
    David Trasbo
    • 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.

    April 14th, 2010 @ 08:19 PM

  6. Ryan Bigg
    Ryan Bigg
    • State changed from new to invalid

    April 15th, 2010 @ 11:01 AM

  7. Brent Greeff
    Brent Greeff
    • 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'

    generates:

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

    December 24th, 2010 @ 02:35 AM

  8. Brent Greeff
    Brent Greeff

    OK, figured it out,

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

    does the trick.

    December 25th, 2010 @ 01:50 AM