Problem with routing 3

I am working on creating an image server that looks like a Flickr server. In my routes file, I:

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

And then, in my opinion, I call the following, but keep getting an ActionController :: RoutingError exception:

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

Error:

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

Update

The query parameters are as follows. It does not appear to detect an underscore between the secret and the identifier. This keeps it all a secret. For example, if I require: http://example.com/image/server/07fde2a9c76de3d16fb70e772d944652_2_n.jpg In my parameters I will get the following:

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

He takes the whole line down to _n and assigns it to a secret, which for some reason causes its 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>' 
+4
source share
2 answers

The problem is that the Routing parser only supports a slash, period, and question mark as a delimiter in its default collection, so it does not see "_" as a valid delimiter, and you get a match with everything but one character for: id when matching. In addition, your parameter is secret, because the router does not recognize _ as a separator and includes it in the name of your segment.

I think your choice is to go from _ to slash (this will at least temporarily confirm if the problem is real). Or write a user restriction as described in sections 3.8 and 3.10 of the routing guide: http://guides.rubyonrails.org/routing.html#advanced-constraints

You can learn more about the corresponding route: http://apidock.com/rails/ActionController/Routing and here https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/routing/mapper.rb

+2
source

The underscore is a valid character for the URL parameter. I hope this solves your problem.

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

Source: https://habr.com/ru/post/1347480/


All Articles