I am using Ruby on Rails 3 and I would like to implement some Rack middleware.
I know (but maybe I am wrong!) that, before of version 3, there was the Rails Metal "system" to handle those. But now?!
Where in my Rails application I have to locate files to use for middleware purposes (before of version 3, if I am not wrong, the folder was named 'metal')? then, how I must state them in the application.rb file?
Check out the Rails on Rack page on Rails Guides.
Your own middlewares can be stored in lib and can be required at the top of application.rb.
Checkout the following blog post, that may answer your question:
http://tektastic.com/2010/07/rails3-rack-and-where-did-my-metal-go.html
Check out this http://www.engineyard.com/blog/2010/rails-and-merb-merge-plugin-api-part-3-of-6/ for how to create a "Metal" controller
http://www.ruby-on-rails-outsourcing.com/articles/2010/05/28/how-to-create-your-own-rack-middleware-class-in-ruby-on-rails/ may be helpful for you.
It basically suggests to put your class in /lib/class_name.rb and then instruct it to be used like
Rails::Initializer.run do |config|
config.middleware.use "ClassName"
...
I believe you are right there is no more "metal" option in rails 3 http://tektastic.com/2010/07/rails3-rack-and-where-did-my-metal-go.html you have to use a rack middleware instead. I'm unsure if this causes a performance hit or not.
Related
I'm trying to get a better handle on Rack middleware, and I've just learned that you can view all of the installed middlewares for a Rails app using rails middleware. In a fresh Rails app, that's about 25 different middlewares.
I'd really like to examine the source code of these different middlewares, but I can't find out where they live in the app. Have they all been compiled to executable, so the only way I can read the source is on Github? If so, how can I find those repos? Thanks!
For the ActionDispatch, ActiveRecord, Rails, ActiveSupport, and Testing middlewares, they'll be in rails/rails, the Rack stuff will be in rack/rack, and WebConsole will be in rails/web-console
They're not compiled into an executable, no. Each gem is downloaded based on the contents of your Gemfile and gets semi-magically loaded via your environment and config.ru.
I've got a Rails project that's setup using Bundler. One of my bundled gems provides a Rack middleware that I'd like to use in my Rails app (but only in the 'production' Rails environment).
If I just put something like this in config/environments/production.rb, I get an unknown constant error:
config.middleware.use ::Rack::MyMiddleware
... presumably because Bundler.require has not yet been called at this point, and none of my bundled gems are available.
I have found a few ways of working around this, but none seem great, and I'm wondering if there's a more standard solution that I'm missing:
Explicitly require 'my_middleware_gem' in config/environments/production.rb
Wrap the config.middleware.use call in an after_initialize block, ensuring that Bundler has a chance to do its thing before I try to reference the constant.
Use a string ("::Rack::MyMiddleware") instead of the bare class constant. This doesn't seem to work with Unicorn for some reason, but does work with some other servers (with Unicorn it ends up trying to call "::Rack::MyMiddleware".new, which of course fails).
Am I missing some better approach? At what point in the initialization process is it expected that bundled gems should be available?
Copying the answer from the comments in order to remove this question from the "Unanswered" filter:
matt suggested:
I think using the after_initialize block is the right way in this case.
grumbler confirmed:
Yeah, that's what I ended up going with. Thanks! Regarding the unicorn issue alluded to in the original question, turns out I was hitting this problem: http://davidvollbracht.com/blog/headachenewunicorn-capistrano-bundler-usr2
Are there any client-side validation gems similar to client_side_validations (https://github.com/bcardarella/client_side_validations) for Rails 2.x?
I am actually in the process of backporting the current ClientSideValidations gem to Rails 2.x
https://github.com/bcardarella/client_side_validations-rails_2
It's not ready for public consumption yet, but you might want to keep an eye on it. It will inherit from the current gem and add the necessary functionality to Rails 2.x.
Here is a project for client side validations for rails 2, it seems to be the same project that the one you refer but it was left in order to give full functionality to rails 3. There is also an example for rails 2.
I recommend to check a great tutorial using jquery validations with rails.
Hope this helps.
I want to store the session data in database using the ActiveRecord::SessionStore module. I have been searching for quite sometime for this without success. Either i am not using the proper search terms or i am being blind to something very obvious.
I have used this statement require ActiveRecord::SessionStore::Session in my code to enable session handling with active record. It conks out with the error uninitialized constant ActiveRecord::ActionDispatch . I assume that i have install the actiondispatch module. Am I correct?
Please bear in mind that this is my first shot with Ruby-Sinatra. I am coming from PHP.
So, what should i use to make Sinatra use database-based sessions using ActiveRecord?
I ended up solving the uninitialized constant ActiveRecord::ActionDispatch error by adding the actionpack gem to my application's Gemfile and requiring action_dispatch. You might also have to require 'logger'
Sinatra is a Rack based application and allows addition of middleware. Middlewares can be plugged into any Rack based framework.
Search on references of config.ru in the Sinatra Book
so you would create a config.ru file in the root of your app.
and put something like this
require 'my_app'
use ActiveRecord::SessionStore
run MyApp
I looked in the Rails docs under inflector and found this message...
Module deprecated
This module is deprecated on the latest stable version of Rails. The last existing version (v2.1.0) is shown here.
There was no explanation or reference to any further detail.
I recalled seeing a Rails Trac website. I hunted that down and found that it too is deprecated. It referred me to Lighthouse.
I found some info there -- the Rails core team is not accepting patches for inflections.rb. But it didn't really explain the deprecation message. What is the story behind that?
You might take a look at this post for an explanation.
I'm looking at the Edge Rails source code for inflector.rb right now and I can't see anything to say that it's deprecated—where did you get that information from?
Patches aren't being accepted because they might break legacy code that's relying on incorrect inflections. You can add your own rules by accessing the singleton instance yielded by Inflector.inflections, as shown below:
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'person', 'people'
inflect.uncountable 'rails'
end
Two separate issues at play. Inflections won't be patched to correct errors to protect legacy uses of incorrect inflections but more importantly inflections was moved into ActiveSupport as of 2.2.1
see 2.2.1 here versus pre 2.2.1 here