francesc / rails-translate-routes

MIT License
147 stars 50 forks source link

Matching regex #11

Closed guiocavalcanti closed 12 years ago

guiocavalcanti commented 12 years ago

I defined my routes as follows:

Redu::Application.routes.draw do
  namespace 'api' do
    match '*anything', :to => 'api#routing_error'
  end
end
ActionDispatch::Routing::Translator.translate_from_file('lang/i18n-routes.yml')

When I try to run rake routes I get this error:

(in /Users/guiocavalcanti/workspace/work/redu)
** Invoke routes (first_time)
** Invoke environment (first_time)
** Execute environment
rake aborted!
can't convert Array into String
/Users/guiocavalcanti/.rvm/gems/ruby-1.8.7-p352/gems/rack-mount-0.6.14/lib/rack/mount/regexp_with_named_groups.rb:31:in `initialize'
/Users/guiocavalcanti/.rvm/gems/ruby-1.8.7-p352/gems/rack-mount-0.6.14/lib/rack/mount/regexp_with_named_groups.rb:31:in `compile'
/Users/guiocavalcanti/.rvm/gems/ruby-1.8.7-p352/gems/rack-mount-0.6.14/lib/rack/mount/regexp_with_named_groups.rb:31:in `initialize'
/Users/guiocavalcanti/.rvm/gems/ruby-1.8.7-p352/gems/rack-mount-0.6.14/lib/rack/mount/regexp_with_named_groups.rb:25:in `new'
/Users/guiocavalcanti/.rvm/gems/ruby-1.8.7-p352/gems/rack-mount-0.6.14/lib/rack/mount/regexp_with_named_groups.rb:25:in `new'
/Users/guiocavalcanti/.rvm/gems/ruby-1.8.7-p352/gems/actionpack-3.0.10/lib/action_dispatch/routing/route.rb:25:in `initialize'
/Users/guiocavalcanti/.rvm/gems/ruby-1.8.7-p352/gems/actionpack-3.0.10/lib/action_dispatch/routing/route_set.rb:186:in `inject'
/Users/guiocavalcanti/.rvm/gems/ruby-1.8.7-p352/gems/actionpack-3.0.10/lib/action_dispatch/routing/route.rb:24:in `each'
/Users/guiocavalcanti/.rvm/gems/ruby-1.8.7-p352/gems/actionpack-3.0.10/lib/action_dispatch/routing/route.rb:24:in `inject'
/Users/guiocavalcanti/.rvm/gems/ruby-1.8.7-p352/gems/actionpack-3.0.10/lib/action_dispatch/routing/route.rb:24:in `initialize'
/Users/guiocavalcanti/.rvm/gems/ruby-1.8.7-p352/gems/actionpack-3.0.10/lib/action_dispatch/routing/route_set.rb:299:in `new'
/Users/guiocavalcanti/.rvm/gems/ruby-1.8.7-p352/gems/actionpack-3.0.10/lib/action_dispatch/routing/route_set.rb:299:in `add_route'
/Users/guiocavalcanti/.rvm/gems/ruby-1.8.7-p352/gems/rails-translate-routes-0.0.4/lib/rails-translate-routes.rb:195:in `translate'
/Users/guiocavalcanti/.rvm/gems/ruby-1.8.7-p352/gems/rails-translate-routes-0.0.4/lib/rails-translate-routes.rb:194:in `each'
/Users/guiocavalcanti/.rvm/gems/ruby-1.8.7-p352/gems/rails-translate-routes-0.0.4/lib/rails-translate-routes.rb:194:in `translate'
/Users/guiocavalcanti/.rvm/gems/ruby-1.8.7-p352/gems/rails-translate-routes-0.0.4/lib/rails-translate-routes.rb:193:in `each'
/Users/guiocavalcanti/.rvm/gems/ruby-1.8.7-p352/gems/rails-translate-routes-0.0.4/lib/rails-translate-routes.rb:193:in `translate'
/Users/guiocavalcanti/.rvm/gems/ruby-1.8.7-p352/gems/rails-translate-routes-0.0.4/lib/rails-translate-routes.rb:347:in `translate_from_file'
/Users/guiocavalcanti/workspace/work/redu/config/routes.rb:6

I'm trying to make a catch all route (inside api namespace) to rescue from RoutingError (see this reference for details).

francesc commented 12 years ago

You'll probably want to setup the api routes only in english as I don't see the point to have it several languages. Define a new Redu::Application.routes.draw do below the translated block (see the readme in backend section).

Hope it helps.

guiocavalcanti commented 12 years ago

I did what you suggested with no success:

Redu::Application.routes.draw do
  root :to => 'base#site_index', :as => :home
end

ActionDispatch::Routing::Translator.translate_from_file('lang/i18n-routes.yml')

Redu::Application.routes.draw do
  namespace 'api' do
    resources :environments, :except => [:new, :edit] do
      resources :courses, :except => [:new, :edit], :shallow => true
      resources :users, :only => :index
    end
    # ActionController::RoutingError
    match '*anything', :to => 'api#routing_error'
  end
end

The strange part is that when I remove the resources part everything works. Do you have any hints about that?

francesc commented 12 years ago

Weird, as the gem shouldn't be doing anything with the second routing block.

Does it work if you remove: ActionDispatch::Routing::Translator.translate_from_file('lang/i18n-routes.yml') line?

guiocavalcanti commented 12 years ago

Maybe the error is caused by the :except => [] part. I setup a fresh rails 3.0.11 project with this routes.rb file:

TranslateRoutesTest::Application.routes.draw do
  resources :environment, :except => [:index, :show]
end

ActionDispatch::Routing::Translator.translate_from_file('config/locales/i18n-routes.yml')

And the same error came out. Removing the except throws no error.

guiocavalcanti commented 12 years ago

By the way, it works removing the line you told me.

francesc commented 12 years ago

Maybe something was broken up when adding support to 3.1 and 3.2 I use plenty of except and only on resources on those versions and it works correctly.

If you're still using 3.0 maybe you want to give a try to https://github.com/raul/translate_routes the original gem we forked in order to have suport for 3.1 and 3.2

bobbus commented 12 years ago

I got the same errors on an application using Rails 3.0, it is probably because of this Rails version.

francesc commented 12 years ago

@bobbus @guiocavalcanti did you try the orginal gem? https://github.com/raul/translate_routes it should work ok for you until you decide to switch to Rails 3.2

guiocavalcanti commented 12 years ago

Yes, it worked fine for me. Thanks for the help. :)