Tag: routes

NoMethodError: undefined method `split’ for true:TrueClass

Weird errors occurs:

NoMethodError: undefined method `split' for true:TrueClass
    /long_path/actionpack-3.1.3/lib/action_dispatch/routing/route_set.rb:360:in `block in <class:Generator>'
    /long_path/rack-mount-0.8.3/lib/rack/mount/generatable_regexp.rb:197:in `call'
    /long_path/rack-mount-0.8.3/lib/rack/mount/generatable_regexp.rb:197:in `parameterize'
    /long_path/rack-mount-0.8.3/lib/rack/mount/generatable_regexp.rb:170:in `block in generate_from_segments'
    /long_path/rack-mount-0.8.3/lib/rack/mount/generatable_regexp.rb:164:in `map'
    /long_path/rack-mount-0.8.3/lib/rack/mount/generatable_regexp.rb:164:in `generate_from_segments'
    /long_path/rack-mount-0.8.3/lib/rack/mount/generatable_regexp.rb:57:in `generate'
    /long_path/rack-mount-0.8.3/lib/rack/mount/route.rb:107:in `generate'
    /long_path/rack-mount-0.8.3/lib/rack/mount/route_set.rb:242:in `block in generate'
    /long_path/rack-mount-0.8.3/lib/rack/mount/route_set.rb:240:in `each'
    /long_path/rack-mount-0.8.3/lib/rack/mount/route_set.rb:240:in `generate'
    /long_path/actionpack-3.1.3/lib/action_dispatch/routing/route_set.rb:453:in `generate'
    /long_path/actionpack-3.1.3/lib/action_dispatch/routing/route_set.rb:494:in `generate'
    /long_path/actionpack-3.1.3/lib/action_dispatch/routing/route_set.rb:490:in `generate_extras'
    /long_path/actionpack-3.1.3/lib/action_dispatch/routing/route_set.rb:486:in `extra_keys'
    /long_path/actionpack-3.1.3/lib/action_controller/test_case.rb:145:in `assign_parameters'
    /long_path/actionpack-3.1.3/lib/action_controller/test_case.rb:438:in `process'
    /long_path/actionpack-3.1.3/lib/action_controller/test_case.rb:49:in `process'
    /long_path/devise-2.0.1/lib/devise/test_helpers.rb:19:in `block in process'
    /long_path/devise-2.0.1/lib/devise/test_helpers.rb:70:in `catch'
    /long_path/devise-2.0.1/lib/devise/test_helpers.rb:70:in `_catch_warden'
    /long_path/devise-2.0.1/lib/devise/test_helpers.rb:19:in `process'
    /long_path/actionpack-3.1.3/lib/action_controller/test_case.rb:370:in `post'

when trying to perform get request in functional tests:

get :smthng

So WTF? This type of error occurs when we have a route with :format:

    post '/' => 'smthing#create', :format => true

but the request is performed without :format option. To fix it, just add :format into your request:

get :smthng, :format => :json

Rails3 i routes.rb – rozbicie (rozłożenie) na kilka plików

Wraz z rozrostem naszej aplikacji, zaczynają pojawiać się w niej przestrzenie nazw/moduły (namespaces). Dzięki takiemu podziałowi możemy zachować logikę w aplikacji dzieląc ją (przykład) np. na:

  • api
  • admin
  • dev

Wygodnie byłoby móc odwzorować taki podział także na pliku routes.rb, który niestety z czasem będzie rósł coraz bardziej. Aby temu zapobiec (i zapanować nad ew. rozrostem), warto rozdzielić plik routes.rb. Jak tego dokonać? Wystarczy utworzyć sobie katalog config/routes w nim utworzyć swoją herarchię plików (może być zagnieżdzona w subkatalogach). Następnie należy dodać poniższe linijki do pliku config/application.rb:

    # Ładuj wszystkie routy z katalogu config/routes
    Dir["#{Rails.root}/config/routes/**/*.rb"].each do |route_file|
      config.paths.config.routes << route_file
    end

Powyższy kod przeszukuje rekurencyjnie katalog z routsami i załącza te na które natrafi.

Copyright © 2024 Closer to Code

Theme by Anders NorenUp ↑