Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a middleware to a controllers middleware stack causes problems with helper inclusion #18555

Closed
nateberkopec opened this issue Jan 16, 2015 · 5 comments

Comments

@nateberkopec
Copy link
Contributor

Given a blank rails app fresh from rails new, add two helper methods:

#app/helpers/application_helper.rb
module ApplicationHelper
  def hello
    "world"
  end
end

#app/helpers/foo_helper.rb
module FooHelper
  def foo
    "bar"
  end
end

Now, add a middleware to the ApplicationController in an environment's config file. For example:

#config/environments/development.rb
ApplicationController.middleware.use("::Rack::Auth::Basic", "Test") do |u, p|
  u == "user" && p == "secret"
end

FooHelper will no longer be included by default in ActionView::Base.

irb(main):001:0> helper.foo
NoMethodError: undefined method `foo' for #<ActionView::Base:0x00000109927778>

Present on 4.1 and 4.2, unsure of other versions. A blank app that reproduces this is here.

@sebjacobs
Copy link
Contributor

You should be able to configure the middleware directly on config.

e.g

Rails.application.configure do
  config.middleware.use("::Rack::Auth::Basic", "Test") do |u, p|
    u == "user" && p == "secret"
  end
end

@nateberkopec
Copy link
Contributor Author

@sebjacobs I could, but in this case, I actually only want to add it to ApplicationController.

@nateberkopec
Copy link
Contributor Author

Add the middleware directly in application_controller.rb and all helpers are included as usual. This isn't super ideal for us - I like keeping environment specific code in config/environments. I may not be able to do this due to load order issues, and if thats the case, then this should be closed.

@rails-bot
Copy link

This issue has been automatically marked as stale because it has not been commented on for at least
three months.

The resources of the Rails team are limited, and so we are asking for your help.

If you can still reproduce this error on the 4-2-stable, 4-1-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

@rafaelfranca
Copy link
Member

It happens because when you call ApplicationController in a initializer it load the ActionController before action_view helpers and hooked. Yeah, it is not possible to use this right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants