ruby on rails - Trouble on routing Racks -
i using ruby on rails 3 , route urls rack middlewares. is, if user try browse @ http://<my_site_name>.com/api/user/1
system should consider run before rack file , proceed in request.
i have rack::api:user located in lib/rack/api/user
folder.
from ror official documentation discovered this:
mount rack-based application used within application. mount somerackapp, :at => "some_route" alternatively: mount(somerackapp => "some_route") mounted applications come routing helpers access them. these named after class specified, above example helper either +some_rack_app_path+ or +some_rack_app_url+. customize helper's name, use +:as+ option: mount(somerackapp => "some_route", :as => "exciting") generate +exciting_path+ , +exciting_url+ helpers can used navigate mounted app.
in routers.rb file tryed
mount "rack::api::user", :at => "/api/user/1" # => argumenterror missing :action scope "/api/user/1" mount "rack::api::user" end # => nomethoderror undefined method `find' "rack::api::user
i tryed
match '/api/user/1' => rack::api::user # => routing error no route matches "/api/user/1" match '/api/user/1', :to => rack::api::user # argumenterror missing :controller
but no 1 works.
update
my rack file this:
module api class user def initialize(app) @app = app end def call(env) if env["path_info"] =~ /^\/api\/user\/i ... else @app.call(env) end end end end
assuming you're require
-ing rack app somewhere in bootup process, in initializer (keep in mind files lib
not automatically loaded anymore unless write code so! see this answer more), try mounting without quotes. example, instead of:
mount "rack::api::user", :at => "/api/user/1"
try
mount rack::api::user, :at => "/api/user/1"
[update]
here link changes made basic rails application demonstrates both autoloading , mounting rack application: https://github.com/binarymuse/so_5100999/compare/master...rack
[update 2]
ah, see you're saying now. want middleware. i've updated code @ above url implement application middleware. config/initializers/rack.rb
file loads , inserts middleware. hope you're looking for!
Comments
Post a Comment