This project is archived and is in readonly mode.

#5342 ✓resolved
Thomas Kadauke

Rails 3.0.0rc does not allow @config instance variable in controllers

Reported by Thomas Kadauke | August 9th, 2010 @ 11:13 PM

I have a class that stores user configuration in my Rails app. While porting to Rails 3, I got a strange error. I was able to isolate the problem. Steps to reproduce:

  • Generate a fresh rails app (3.0.0rc)

  • Add a model configuration.rb (not ActiveRecord!):

  class Configuration
  end
  • Add a controller settings_controller.rb:
  class SettingsController < ApplicationController
    def index
      @config = Configuration.new
    end
  end
  • Add an empty view index.html.erb

  • Add settings to routes.rb:

  resources :settings
  • Add this simple test:
  require 'test_helper'

  class SettingsControllerTest < ActionController::TestCase
    test "boom" do
      get :index
    end
  end

When run, the test outputs the following error (see below for the full backtrace):

  1) Error:
  test_boom(SettingsControllerTest):
  NoMethodError: undefined method `protected_instance_variables' for #<Configuration:0x2878258>
  method test_boom in settings_controller_test.rb at line 6

When I change the name of the instance variable, everything works fine. From a developer's perspective, if @config is not allowed as an instance variable name in controllers, there should be at least a warning. On the other hand, reserving very common instance variable names such as @config seems a bit too restrictive and should at least be well documented.

I also noticed that the constant name ::Config seems to be reserved by Rails 3.0.0rc; I think the same applies here.

If desired, I can push my test application to github, just let me know.

Here's the full backtrace:

    1) Error:
  test_boom(SettingsControllerTest):
  NoMethodError: undefined method `protected_instance_variables' for #<Configuration:0x2870ecc>
  method protected_instance_variables in activesupport (3.0.0.rc) lib/active_support/configurable.rb at line 22
  method view_assigns in actionpack (3.0.0.rc) lib/abstract_controller/rendering.rb at line 130
  method view_context in actionpack (3.0.0.rc) lib/abstract_controller/rendering.rb at line 86
  method _render_template in actionpack (3.0.0.rc) lib/abstract_controller/rendering.rb at line 114
  method render_to_body in actionpack (3.0.0.rc) lib/abstract_controller/rendering.rb at line 108
  method render_to_body in actionpack (3.0.0.rc) lib/action_controller/metal/renderers.rb at line 47
  method render_to_body in actionpack (3.0.0.rc) lib/action_controller/metal/compatibility.rb at line 55
  method render_to_string in actionpack (3.0.0.rc) lib/abstract_controller/rendering.rb at line 101
  method render in actionpack (3.0.0.rc) lib/abstract_controller/rendering.rb at line 92
  method render in actionpack (3.0.0.rc) lib/action_controller/metal/rendering.rb at line 17
  method render in actionpack (3.0.0.rc) lib/action_controller/metal/instrumentation.rb at line 40
  method ms in activesupport (3.0.0.rc) lib/active_support/core_ext/benchmark.rb at line 5
  method realtime in benchmark.rb at line 308
  method ms in activesupport (3.0.0.rc) lib/active_support/core_ext/benchmark.rb at line 5
  method render in actionpack (3.0.0.rc) lib/action_controller/metal/instrumentation.rb at line 40
  method cleanup_view_runtime in actionpack (3.0.0.rc) lib/action_controller/metal/instrumentation.rb at line 78
  method cleanup_view_runtime in activerecord (3.0.0.rc) lib/active_record/railties/controller_runtime.rb at line 15
  method render in actionpack (3.0.0.rc) lib/action_controller/metal/instrumentation.rb at line 39
  method default_render in actionpack (3.0.0.rc) lib/action_controller/metal/implicit_render.rb at line 10
  method send_action in actionpack (3.0.0.rc) lib/action_controller/metal/implicit_render.rb at line 5
  method process_action in actionpack (3.0.0.rc) lib/abstract_controller/base.rb at line 136
  method process_action in actionpack (3.0.0.rc) lib/action_controller/metal/rendering.rb at line 11
  method process_action in actionpack (3.0.0.rc) lib/abstract_controller/callbacks.rb at line 18
  method _run__987036831__process_action__199225275__callbacks in activesupport (3.0.0.rc) lib/active_support/callbacks.rb at line 429
  method send in activesupport (3.0.0.rc) lib/active_support/callbacks.rb at line 404
  method _run_process_action_callbacks in activesupport (3.0.0.rc) lib/active_support/callbacks.rb at line 404
  method send in activesupport (3.0.0.rc) lib/active_support/callbacks.rb at line 93
  method run_callbacks in activesupport (3.0.0.rc) lib/active_support/callbacks.rb at line 93
  method process_action in actionpack (3.0.0.rc) lib/abstract_controller/callbacks.rb at line 17
  method process_action in actionpack (3.0.0.rc) lib/action_controller/metal/instrumentation.rb at line 30
  method instrument in activesupport (3.0.0.rc) lib/active_support/notifications.rb at line 52
  method instrument in activesupport (3.0.0.rc) lib/active_support/notifications/instrumenter.rb at line 21
  method instrument in activesupport (3.0.0.rc) lib/active_support/notifications.rb at line 52
  method process_action in actionpack (3.0.0.rc) lib/action_controller/metal/instrumentation.rb at line 29
  method process_action in actionpack (3.0.0.rc) lib/action_controller/metal/rescue.rb at line 17
  method process in actionpack (3.0.0.rc) lib/abstract_controller/base.rb at line 105
  method process in actionpack (3.0.0.rc) lib/abstract_controller/rendering.rb at line 40
  method process_with_new_base_test in actionpack (3.0.0.rc) lib/action_controller/metal/testing.rb at line 12
  method process in actionpack (3.0.0.rc) lib/action_controller/test_case.rb at line 404
  method get in actionpack (3.0.0.rc) lib/action_controller/test_case.rb at line 342
  method test_boom in settings_controller_test.rb at line 6
  method __send__ in activesupport (3.0.0.rc) lib/active_support/testing/setup_and_teardown.rb at line 67
  method run in activesupport (3.0.0.rc) lib/active_support/testing/setup_and_teardown.rb at line 67
  method _run_setup_callbacks in activesupport (3.0.0.rc) lib/active_support/callbacks.rb at line 428
  method run in activesupport (3.0.0.rc) lib/active_support/testing/setup_and_teardown.rb at line 65

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Referenced by

Pages