Where's ActionController::Dispatcher in Rails 3.2? - ruby-on-rails

I'm following an example in The Rails 3 way, and when I tried to send a request to dispatcher in the console, with ActionController::Dispatcher.new.call(env).last.body
I got
1.9.3p194 :001 > ActionController::Dispatcher.new.call(env).last.body
NameError: uninitialized constant ActionController::Dispatcher'
I was using rails 3.2.6, I checked the rails api and found out they removed dispatcher in the ActionController, but the rails guide said:ActionController::Dispatcher.new is the primary Rack application object of a Rails application. Any Rack compliant web server should be using ActionController::Dispatcher.new object to serve a Rails application.
I find the v3.0.7 of rails api, the Dispatcher still exist in that version.
So, here are my questions: how can I find the equivalent methods serve as ActionController.Dispatcher.new? and given that my apps work well with rails 3.2.6, which part of rails plays the role as ActionController.Dispatcher now?

This is what the Rails edge guides say about primary Rack application objects:
ApplicationName::Application is the primary Rack application object of a Rails application. Any Rack compliant web server should be using ApplicationName::Application object to serve a Rails application.
So ActionController::Dispatcher.new has just been replaced by ApplicationName::Application. I'm not sure when they made this switch. This worked for me:
Blog::Application.call(env).last.body
(The first few lines after NoMethodError: undefined method 'key?' for nil:NilClass tell us that the key? method is being called in actionpack-3.2.11/lib/action_dispatch/routing/route_set.rb. Working backwards from there, we can see that the variable
params = env['action_dispatch.request.path_parameters']
is nil when it shouldn't be. So we can set env['action_dispatch.request.path_parameters'] in the console, but that leads to another NoMethodError on a nil object set in the initialize method. So we could probably fix that by passing in an options hash to Dispatcher.new, but it's probably best just to use ApplicationName::Application.)

It has been moved to ActionDispatch (actionpack/lib/action_dispatch) and can be found here: actionpack/lib/action_dispatch/routing/route_set.rb, class called
ActionController::Routing::RouteSet::Dispatcher.new

Related

'load_missing_constant' when trying to create an observer on Rails

I am trying to create an observer for my Offer model but I keep getting this error:
/Users/codus/.rvm/gems/ruby-1.9.3-p194#gyp/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:503:in `load_missing_constant': Expected /Users/codus/Projetos/gyp-revolution/app/models/offer.rb to define Offer (LoadError)
There is nothing special about my model and I am sure it is declared in the right place (the application works fine without the observer).
I've generated my observer with the Rails script
rails g observer offer
And I added this in my config/application.rb file
config.active_record.observers = :offer_observer
The strangest part is that it works fine with all my other models.
I am using Rails 3.2.6 with Ruby 1.9.3p194.
The problem was that I was using FactoryGirl, and inside my Offer factory I called a constant defined in my model. The factory was being loaded before the model, so this error was happening.
It's weird that the observer changed this, it works fine without it. Now I just use the value in my factory, not the constant.

NameError for ActionMailer within ActionController

I'm on Rails 3.0, and I pretty much followed the steps on the API to define a mailer by doing
rails generate mailer Notifier
Within my controller, I call Notifier.mytemplate(...)
When I go to the page, I see an error:
uninitialized constant MyController::Notifier
Seems like something is not getting initialized correctly, and it's not realizing that Notifier is already defined in app/mailers/notifier.rb

How to Use OmniAuth Properly with Rails 2

I am trying to use an OmniAuth (0.2.6) strategy for my application. The architecture is Rails 2.3.10 and Rack is version is 1.1 (this version or higher is required for OmniAuth). A problem that occurs is Rails doesn't recognize my redirect to "/auth/provider". The actual error message is "No route matches "/auth/casport". Even though that error gets thrown, the code seems to execute past that point up to this error: "request.env['omniauth.auth']", which I'm pretty sure means it doesn't recognize the "omniauth.auth" key for the env hash. I have a feeling that OmniAuth isn't being loaded properly. I've tested the same strategy and code base with Mongrel and Webrick, and I get identical error messages.
Here is my OmniAuth initializer (config/initializers/omniauth.rb):
ActionController::Dispatcher.middleware.use OmniAuth::Builder do
provider :casport, :setup => true
end
I definitely have routes in places for auth/casport/setup and auth/casport/callback.
I've also tried this approach: How do you implement OmniAuth on Rails 2.2?
config.middleware.use OmniAuth::Builder do
provider :casport, :setup => true
end
by placing that middleware code in the config/environments/development.rb file.
Does anyone have any thoughts on what I could be doing incorrectly? Thanks!
Edit: I've also tried this approach Has anyone used omniauth with rails 2.3.8?
ActionController::Dispatcher.middleware.use OmniAuth::Strategies::Casport = {
:setup => true
}
and that returns a read error: "NoMethodError: undefined method 'new' for #Hash...
Edit 2: I've upgraded to Rails 2.3.10 as OmniAuth isn't compatible with Rails 2.3.4.
I created an issue on OmniAuth's Github page https://github.com/intridea/omniauth/issues/397, with the above details and this is the response from Michael Bleigh, the creator of OmniAuth:
"OmniAuth has never officially supported Rails 2.3.x -- I've heard that some people have had luck with it but it's not something that is guaranteed. Sorry!"
So it looks like the methods I posted above may work for some folks. To guarantee correct operation, Rails 3 is needed.

How to get the list of all engines in Rails 3 app

According to Rails engines extending functionality in Rails 2.x one could do
Rails::Initializer.new(Rails.configuration).plugin_loader.engines
This code is not working in Rails 3
ActionController::RoutingError (undefined method `new' for Rails::Initializer:Module):
config/application.rb:12:in `require_or_load'
What do I need to do in Rails 3 to get such list of engines?
This is needed for Extending controllers of a Rails 3 Engine in the main app
This has changed with Rails 4.1. The accepted answer is deprecated and the new way to get the installed Engines for a Rails application is now:
::Rails::Engine.subclasses.map(&:instance)
Here's a reference to the commit in github making the change (and also showing how it was implemented after initial deprecation...)
If you need to use the previous solution from Rails 4.1:
module Rails
class Engine
class Railties
def self.engines
#engines ||= Rails::Engine.subclasses.map(&:instance)
end
end
end
end
As of 5/10/2011 and Rails 3.1 beta, it's now
Rails::Application::Railties.engines
Try:
Rails::Application.railties.engines

Rails and attr_accessible: is there a way to raise an exception if a non-mass-assignable attribute is mass-assigned?

Is there a way to have rails raise an error if an attempt is made to mass-assign attributes that aren't allowed by attr_accessible?
This would be handy in development to remind me why my shiny new model isn't working, and also good to log in production in order to detect malicious activity.
I'm using rails 2.3.8 but will probably soon be migrating to 3.
As of Rails 3.2 this no longer requires monkeypatching -- rails provides this behavior now. Put this in development.rb and test.rb:
config.active_record.mass_assignment_sanitizer = :strict
I would suggest something like the Bento project has incorporated into their Rails app.
They create a Rails Initializer under config/initializers/ and then override the appropriate method in the ActiveModel class to raise a MassAssignmentError (within non-production environments).
I am not sure if this would work, but you could write a test to see if your object "respond_to(:unexpected_attr)". You can then tried to force feed it that attr
Alex

Resources