Rails 3路由问题



我正在制作一个类似于Flickr的图像服务器。在我的路由文件中我有:

get "/image/server/:secret_:id(_:size).:format", :to => "api/images#server", :as => :api_image

然后在我看来,我调用以下,但不断得到一个ActionController::RoutingError异常:

api_image_path({ :secret=>"07fde2a9c76de3d16fb70e772d944652", :id=>2, :size=>"t", :format=>"png" })
错误:

No route matches {:controller=>"api/images", :action=>"server", :secret=>"07fde2a9c76de3d16fb70e772d944652", :id=>2, :size=>"t", :format=>"png"}

Request参数如下。它似乎没有检测到secret和id之间的下划线。这一切都是秘密。例如,如果我请求:http://example.com/image/server/07fde2a9c76de3d16fb70e772d944652_2_n.jpg,我将在我的参数中得到以下内容:

{"secret_"=>"07fde2a9c76de3d16fb70e772d944652_2_",
 "id"=>"n",
 "format"=>"jpg"}

它取_n之前的整个字符串并将其赋值给secret,出于某种原因它称之为secret_

ActionController::RoutingError (Full Trace)

actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:424:in `raise_routing_error'
actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:397:in `generate'
actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:453:in `generate'
actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:481:in `url_for'
actionpack (3.0.3) lib/action_dispatch/routing/url_for.rb:131:in `url_for'
actionpack (3.0.3) lib/action_view/helpers/url_helper.rb:99:in `url_for'
actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:195:in `api_image_url'
app/views/services/about.html.erb:8:in `_app_views_services_about_html_erb__1824827504506770107_2158066880_1802331327662141069'
actionpack (3.0.3) lib/action_view/template.rb:135:in `block in render'
activesupport (3.0.3) lib/active_support/notifications.rb:54:in `instrument'
actionpack (3.0.3) lib/action_view/template.rb:127:in `render'
actionpack (3.0.3) lib/action_view/render/rendering.rb:59:in `block in _render_template'
activesupport (3.0.3) lib/active_support/notifications.rb:52:in `block in instrument'
activesupport (3.0.3) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.0.3) lib/active_support/notifications.rb:52:in `instrument'
actionpack (3.0.3) lib/action_view/render/rendering.rb:56:in `_render_template'
actionpack (3.0.3) lib/action_view/render/rendering.rb:26:in `render'
actionpack (3.0.3) lib/abstract_controller/rendering.rb:114:in `_render_template'
actionpack (3.0.3) lib/abstract_controller/rendering.rb:108:in `render_to_body'
actionpack (3.0.3) lib/action_controller/metal/renderers.rb:47:in `render_to_body'
actionpack (3.0.3) lib/action_controller/metal/compatibility.rb:55:in `render_to_body'
actionpack (3.0.3) lib/abstract_controller/rendering.rb:101:in `render_to_string'
actionpack (3.0.3) lib/abstract_controller/rendering.rb:92:in `render'
actionpack (3.0.3) lib/action_controller/metal/rendering.rb:17:in `render'
actionpack (3.0.3) lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
activesupport (3.0.3) lib/active_support/core_ext/benchmark.rb:5:in `block in ms'
/usr/local/Cellar/ruby/1.9.2-p0/lib/ruby/1.9.1/benchmark.rb:309:in `realtime'
activesupport (3.0.3) lib/active_support/core_ext/benchmark.rb:5:in `ms'
actionpack (3.0.3) lib/action_controller/metal/instrumentation.rb:40:in `block in render'
actionpack (3.0.3) lib/action_controller/metal/instrumentation.rb:78:in `cleanup_view_runtime'
activerecord (3.0.3) lib/active_record/railties/controller_runtime.rb:15:in `cleanup_view_runtime'
thinking-sphinx (2.0.3) lib/thinking_sphinx/action_controller.rb:12:in `cleanup_view_runtime'
actionpack (3.0.3) lib/action_controller/metal/instrumentation.rb:39:in `render'
actionpack (3.0.3) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (3.0.3) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.0.3) lib/abstract_controller/base.rb:151:in `process_action'
actionpack (3.0.3) lib/action_controller/metal/rendering.rb:11:in `process_action'
actionpack (3.0.3) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.0.3) lib/active_support/callbacks.rb:456:in `block in _run__3222905760235059467__process_action__3328463717343760166__callbacks'
activesupport (3.0.3) lib/active_support/callbacks.rb:220:in `block in _conditional_callback_around_188'
activesupport (3.0.3) lib/active_support/callbacks.rb:330:in `around'
activesupport (3.0.3) lib/active_support/callbacks.rb:314:in `_callback_around_35'
activesupport (3.0.3) lib/active_support/callbacks.rb:219:in `_conditional_callback_around_188'
activesupport (3.0.3) lib/active_support/callbacks.rb:435:in `_run__3222905760235059467__process_action__3328463717343760166__callbacks'
activesupport (3.0.3) lib/active_support/callbacks.rb:409:in `_run_process_action_callbacks'
activesupport (3.0.3) lib/active_support/callbacks.rb:93:in `run_callbacks'
actionpack (3.0.3) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.0.3) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.0.3) lib/active_support/notifications.rb:52:in `block in instrument'
activesupport (3.0.3) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.0.3) lib/active_support/notifications.rb:52:in `instrument'
actionpack (3.0.3) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.0.3) lib/action_controller/metal/rescue.rb:17:in `process_action'
actionpack (3.0.3) lib/abstract_controller/base.rb:120:in `process'
actionpack (3.0.3) lib/abstract_controller/rendering.rb:40:in `process'
actionpack (3.0.3) lib/action_controller/metal.rb:138:in `dispatch'
actionpack (3.0.3) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.0.3) lib/action_controller/metal.rb:178:in `block in action'
actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:62:in `call'
actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:62:in `dispatch'
actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:27:in `call'
rack-mount (0.6.14) lib/rack/mount/route_set.rb:148:in `block in call'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:93:in `block in recognize'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:75:in `optimized_each'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:92:in `recognize'
rack-mount (0.6.14) lib/rack/mount/route_set.rb:139:in `call'
actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:492:in `call'
oa-core (0.2.1) lib/omniauth/strategy.rb:55:in `call!'
oa-core (0.2.1) lib/omniauth/strategy.rb:22:in `call'
oa-core (0.2.1) lib/omniauth/strategy.rb:55:in `call!'
oa-core (0.2.1) lib/omniauth/strategy.rb:22:in `call'
oa-core (0.2.1) lib/omniauth/strategy.rb:55:in `call!'
oa-core (0.2.1) lib/omniauth/strategy.rb:22:in `call'
oa-core (0.2.1) lib/omniauth/builder.rb:30:in `call'
lib/flash_session_cookie_middleware.rb:20:in `call'
warden (1.0.3) lib/warden/manager.rb:35:in `block in call'
warden (1.0.3) lib/warden/manager.rb:34:in `catch'
warden (1.0.3) lib/warden/manager.rb:34:in `call'
actionpack (3.0.3) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
actionpack (3.0.3) lib/action_dispatch/middleware/head.rb:14:in `call'
rack (1.2.2) lib/rack/methodoverride.rb:24:in `call'
actionpack (3.0.3) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.0.3) lib/action_dispatch/middleware/flash.rb:182:in `call'
actionpack (3.0.3) lib/action_dispatch/middleware/session/abstract_store.rb:149:in `call'
lib/flash_session_cookie_middleware.rb:20:in `call'
actionpack (3.0.3) lib/action_dispatch/middleware/cookies.rb:295:in `call'
activerecord (3.0.3) lib/active_record/query_cache.rb:32:in `block in call'
activerecord (3.0.3) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in `cache'
activerecord (3.0.3) lib/active_record/query_cache.rb:12:in `cache'
activerecord (3.0.3) lib/active_record/query_cache.rb:31:in `call'
activerecord (3.0.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:353:in `call'
actionpack (3.0.3) lib/action_dispatch/middleware/callbacks.rb:46:in `block in call'
activesupport (3.0.3) lib/active_support/callbacks.rb:415:in `_run_call_callbacks'
actionpack (3.0.3) lib/action_dispatch/middleware/callbacks.rb:44:in `call'
rack (1.2.2) lib/rack/sendfile.rb:107:in `call'
actionpack (3.0.3) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.0.3) lib/action_dispatch/middleware/show_exceptions.rb:46:in `call'
railties (3.0.3) lib/rails/rack/logger.rb:13:in `call'
rack (1.2.2) lib/rack/runtime.rb:17:in `call'
activesupport (3.0.3) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.2.2) lib/rack/lock.rb:11:in `block in call'
<internal:prelude>:10:in `synchronize'
rack (1.2.2) lib/rack/lock.rb:11:in `call'
actionpack (3.0.3) lib/action_dispatch/middleware/static.rb:30:in `call'
railties (3.0.3) lib/rails/application.rb:168:in `call'
railties (3.0.3) lib/rails/application.rb:77:in `method_missing'
passenger (3.0.2) lib/phusion_passenger/rack/request_handler.rb:96:in `process_request'
passenger (3.0.2) lib/phusion_passenger/abstract_request_handler.rb:513:in `accept_and_process_next_request'
passenger (3.0.2) lib/phusion_passenger/abstract_request_handler.rb:274:in `main_loop'
passenger (3.0.2) lib/phusion_passenger/rack/application_spawner.rb:205:in `start_request_handler'
passenger (3.0.2) lib/phusion_passenger/rack/application_spawner.rb:170:in `block in handle_spawn_application'
passenger (3.0.2) lib/phusion_passenger/utils.rb:479:in `safe_fork'
passenger (3.0.2) lib/phusion_passenger/rack/application_spawner.rb:165:in `handle_spawn_application'
passenger (3.0.2) lib/phusion_passenger/abstract_server.rb:357:in `server_main_loop'
passenger (3.0.2) lib/phusion_passenger/abstract_server.rb:206:in `start_synchronously'
passenger (3.0.2) lib/phusion_passenger/abstract_server.rb:180:in `start'
passenger (3.0.2) lib/phusion_passenger/rack/application_spawner.rb:128:in `start'
passenger (3.0.2) lib/phusion_passenger/spawn_manager.rb:253:in `block (2 levels) in spawn_rack_application'
passenger (3.0.2) lib/phusion_passenger/abstract_server_collection.rb:132:in `lookup_or_add'
passenger (3.0.2) lib/phusion_passenger/spawn_manager.rb:246:in `block in spawn_rack_application'
passenger (3.0.2) lib/phusion_passenger/abstract_server_collection.rb:82:in `block in synchronize'
<internal:prelude>:10:in `synchronize'
passenger (3.0.2) lib/phusion_passenger/abstract_server_collection.rb:79:in `synchronize'
passenger (3.0.2) lib/phusion_passenger/spawn_manager.rb:244:in `spawn_rack_application'
passenger (3.0.2) lib/phusion_passenger/spawn_manager.rb:137:in `spawn_application'
passenger (3.0.2) lib/phusion_passenger/spawn_manager.rb:275:in `handle_spawn_application'
passenger (3.0.2) lib/phusion_passenger/abstract_server.rb:357:in `server_main_loop'
passenger (3.0.2) lib/phusion_passenger/abstract_server.rb:206:in `start_synchronously'
passenger (3.0.2) helper-scripts/passenger-spawn-server:99:in `<main>'

问题在于路由解析器只支持正斜杠,点和问号作为默认集合中的分隔符,因此它不会将'_'视为有效的分隔符,并且当您匹配时,除了:id的一个字符之外,您将获得匹配。另外,你的参数是secret_,因为路由器不识别_作为分隔符,并将它包含在你的段名中。

我认为你的选择是从_移动到正斜杠(这至少会暂时确认问题是否存在)。或者按照路由指南的3.8节和3.10节的描述编写自定义约束:http://guides.rubyonrails.org/routing.html#advanced-constraints

你可以在这里阅读更多的路由匹配:http://apidock.com/rails/ActionController/Routing这里https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/routing/mapper.rb

下划线是URL参数的有效字符。我希望这能解决你的问题

get "/image/server/:secret_:id(_:size).:format", :to => "api/images#server", :as => :api_image, :contraints => {:secret => /[wd]+/, :id => /[d]+/, :size => /w/}

相关内容

  • 没有找到相关文章

最新更新