Friday, 15 January 2010

ruby - My rack middleware in Rails 4 not executing -



ruby - My rack middleware in Rails 4 not executing -

i trying build cms application using ruby on rails 4.inorder proper controller.i need preprocess querystring before using it in match method shown below

# config/routes.rb cms::application.routes varparameter=env["query_string"] @controller=preprocessqstring(varparameter) match '/cms/home', to: 'login#show', via: [:get, :post] match 'cms/:content(/:action(/:id(.:format)))',to: @controller+'#show', via: [:get, :post] end

to set env["query_string"] using next rack middleware

# lib/httpvariables.rb require 'rack' class httpvariables def initialize(app) @app = app end def call(env) @status, @headers, @response = @app.call(env) request=rack::request.new(env) env["request_uri"]=request.env["request_uri"] env["query_string"]=request.env["query_string"] env["server_port"]=request.env["server_port"] env["server_protocol"]=request.env["server_protocol"] [@status, @headers, self] end end

i have added line in application.rb

# config/application.rb config.middleware.use rack::httpvariables

to suprise if seek start rails server it's failing showing:

mundile@mundile-hp:~/ror-workspace/workspace/cms$ rails server => booting webrick => rails 4.1.1 application starting in development on http://0.0.0.0:3000 => run `rails server -h` more startup options => notice: server listening on interfaces (0.0.0.0). consider using 127.0.0.1 (--binding option) => ctrl-c shutdown server exiting /home/mundile/ror-workspace/workspace/cms/config/routes.rb:68:in `block in <top (required)>': undefined method `+' nil:nilclass (nomethoderror)

i need help out there identify cause of problem. seems env["query_string"] giving nil why ? thanks.

i changed middleware code to:

# lib/httpvariables.rb require 'rack' class httpvariables def initialize(app) @app = app end def call(env) request=rack::request.new(env) env["request_uri"]=request.env["request_uri"] env["query_string"]=request.env["query_string"] env["server_port"]=request.env["server_port"] env["base_url"]=request.base_url @app.call(env) end end

this works charm. have used code reply question: rails access request in routes.rb dynamic routes

ruby ruby-on-rails-4 rack

No comments:

Post a Comment