undefined method `[]' for false:FalseClass in link_to

1k views Asked by At

I upgraded my rails 3.0 app to 3.1 (and then 3.2 hoping it would resolve this - it didn't). Ever since the upgrade the link_to function in my views fail if I include a :controller and :action

%li
    =link_to "foo",:controller => "utils", :action =>  'my_controller_action'

Here's the stack-trace I get:

NoMethodError - undefined method `[]' for false:FalseClass:
  actionpack (3.2.22) lib/action_controller/metal/url_for.rb:36:in `url_options'
  actionpack (3.2.22) lib/action_view/helpers/url_helper.rb:37:in `url_options'
  actionpack (3.2.22) lib/action_dispatch/routing/url_for.rb:148:in `url_for'
  actionpack (3.2.22) lib/action_view/helpers/url_helper.rb:107:in `url_for'
  actionpack (3.2.22) lib/action_view/helpers/url_helper.rb:242:in `link_to'
  app/views/utils/index.html.haml:27:in `_app_views_utils_index_html_haml___1636103521089972327_70322425585740'
  actionpack (3.2.22) lib/action_view/template.rb:145:in `block in render'
  activesupport (3.2.22) lib/active_support/notifications.rb:125:in `instrument'
  actionpack (3.2.22) lib/action_view/template.rb:143:in `render'
  actionpack (3.2.22) lib/action_view/renderer/template_renderer.rb:47:in `block (2 levels) in render_template'
  actionpack (3.2.22) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
  activesupport (3.2.22) lib/active_support/notifications.rb:123:in `block in instrument'
  activesupport (3.2.22) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (3.2.22) lib/active_support/notifications.rb:123:in `instrument'
  actionpack (3.2.22) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
  actionpack (3.2.22) lib/action_view/renderer/template_renderer.rb:46:in `block in render_template'
  actionpack (3.2.22) lib/action_view/renderer/template_renderer.rb:54:in `render_with_layout'
  actionpack (3.2.22) lib/action_view/renderer/template_renderer.rb:45:in `render_template'
  actionpack (3.2.22) lib/action_view/renderer/template_renderer.rb:18:in `render'
  actionpack (3.2.22) lib/action_view/renderer/renderer.rb:36:in `render_template'
  actionpack (3.2.22) lib/action_view/renderer/renderer.rb:17:in `render'
  actionpack (3.2.22) lib/abstract_controller/rendering.rb:110:in `_render_template'
  actionpack (3.2.22) lib/action_controller/metal/streaming.rb:225:in `_render_template'
  actionpack (3.2.22) lib/abstract_controller/rendering.rb:103:in `render_to_body'
  actionpack (3.2.22) lib/action_controller/metal/renderers.rb:28:in `render_to_body'
  actionpack (3.2.22) lib/action_controller/metal/compatibility.rb:50:in `render_to_body'
  actionpack (3.2.22) lib/abstract_controller/rendering.rb:88:in `render'
  actionpack (3.2.22) lib/action_controller/metal/rendering.rb:16:in `render'
  actionpack (3.2.22) lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
...

The route and method is there (it worked fine in 3.0). It works if I hard-code the URL.

When I debugged the code I get to: lib/action_controller/metal/url_for.rb

module ActionController
  module UrlFor
    extend ActiveSupport::Concern

    include AbstractController::UrlFor

    def url_options
      @_url_options ||= super.reverse_merge(
        :host => request.host,
        :port => request.optional_port,
        :protocol => request.protocol,
        :_path_segments => request.symbolized_path_parameters
      ).freeze

      if _routes.equal?(env["action_dispatch.routes"])
        @_url_options.dup.tap do |options|
          options[:script_name] = request.script_name.dup
          options.freeze
        end
      else
        @_url_options
      end
    end

  end
end

When I inspect if _routes.equal?(env["action_dispatch.routes"]), env is false instead of the usual array of routes...

Any ideas?


Updated with requested info:

 rake routes |grep utils
                 utils_switch_redeemable_to_managed GET    /utils/switch_redeemable_to_managed(.:format)                       utils#switch_redeemable_to_managed
                                      utils_viewlog GET    /utils/viewlog(.:format)                                            utils#viewlog
                        utils_enable_device_polling GET    /utils/enable_device_polling(.:format)                              utils#enable_device_polling
                        utils_fetch_all_dep_devices GET    /utils/fetch_all_dep_devices(.:format)                              utils#fetch_all_dep_devices
                     utils_build_apps_from_licenses GET    /utils/build_apps_from_licenses(.:format)                           utils#build_apps_from_licenses
                      utils_update_vpp_license_info GET    /utils/update_vpp_license_info(.:format)                            utils#update_vpp_license_info
                         utils_update_vpp_user_info GET    /utils/update_vpp_user_info(.:format)                               utils#update_vpp_user_info
                            utils_update_app_prices GET    /utils/update_app_prices(.:format)                                  utils#update_app_prices
                    utils_clean_bad_license_imports GET    /utils/clean_bad_license_imports(.:format)                          utils#clean_bad_license_imports
                   utils_fix_stuck_license_download GET    /utils/fix_stuck_license_download(.:format)                         utils#fix_stuck_license_download
                                   utils_change_log GET    /utils/change_log(.:format)                                         utils#change_log
                                          utils_env GET    /utils/env(.:format)                                                utils#env
                                     utils_evaluate GET    /utils/evaluate(.:format)                                           utils#evaluate
                                                    POST   /utils/evaluate(.:format)                                           utils#evaluate
                               utils_restore_backup GET    /utils/restore_backup(.:format)                                     utils#restore_backup
                                  utils_make_backup GET    /utils/make_backup(.:format)                                        utils#make_backup
                                   utils_where_am_i GET    /utils/where_am_i(.:format)                                         utils#where_am_i
                                 utils_system_stats GET    /utils/system_stats(.:format)                                       utils#system_stats
                     utils_clear_completed_mdm_jobs GET    /utils/clear_completed_mdm_jobs(.:format)                           utils#clear_completed_mdm_jobs
                 utils_clear_old_completed_mdm_jobs GET    /utils/clear_old_completed_mdm_jobs(.:format)                       utils#clear_old_completed_mdm_jobs
                           utils_clear_old_mdm_jobs GET    /utils/clear_old_mdm_jobs(.:format)                                 utils#clear_old_mdm_jobs
                       utils_find_missing_app_names GET    /utils/find_missing_app_names(.:format)                             utils#find_missing_app_names
                      utils_find_missing_bundle_ids GET    /utils/find_missing_bundle_ids(.:format)                            utils#find_missing_bundle_ids
                              utils_data_validation GET    /utils/data_validation(.:format)                                    utils#data_validation
                      utils_truncate_sessions_table GET    /utils/truncate_sessions_table(.:format)                            utils#truncate_sessions_table
                  utils_truncate_delayed_jobs_table GET    /utils/truncate_delayed_jobs_table(.:format)                        utils#truncate_delayed_jobs_table
                 utils_fix_invalid_pool_memberships GET    /utils/fix_invalid_pool_memberships(.:format)                       utils#fix_invalid_pool_memberships
          utils_remove_duplicate_device_enrollments GET    /utils/remove_duplicate_device_enrollments(.:format)                utils#remove_duplicate_device_enrollments
      utils_fix_licenses_of_legacy_orders_for_users GET    /utils/fix_licenses_of_legacy_orders_for_users(.:format)            utils#fix_licenses_of_legacy_orders_for_users
                          utils_test_email_accepted GET    /utils/test_email_accepted(.:format)                                utils#test_email_accepted
                             utils_harvest_licenses GET    /utils/harvest_licenses(.:format)                                   utils#harvest_licenses
                        utils_reset_table_sequences GET    /utils/reset_table_sequences(.:format)                              utils#reset_table_sequences
                                utils_fix_pool_apps GET    /utils/fix_pool_apps(.:format)                                      utils#fix_pool_apps
                            utils_fix_license_costs GET    /utils/fix_license_costs(.:format)                                  utils#fix_license_costs
                             utils_update_app_icons GET    /utils/update_app_icons(.:format)                                   utils#update_app_icons
                                        utils_index GET    /utils/index(.:format)                                              utils#index
                             utils_check_order_data GET    /utils/check_order_data(.:format)                                   utils#check_order_data
                         utils_check_order_data_fix GET    /utils/check_order_data_fix(.:format)                               utils#check_order_data_fix

relevant parts of routes.rb:

  get "/utils/switch_redeemable_to_managed"

  get "/utils/viewlog"

  get "/utils/enable_device_polling"

  get "/utils/fetch_all_dep_devices"

  get "/utils/build_apps_from_licenses"

  get "/utils/update_vpp_license_info"

  get "/utils/update_vpp_user_info"

  get "/utils/update_app_prices"

  get "/utils/clean_bad_license_imports"

  get "/utils/fix_stuck_license_download"

  get "/utils/change_log"

  get "/utils/env"

  get "/utils/evaluate"

  post "/utils/evaluate"

  get "/utils/restore_backup"

  get "/utils/make_backup"

  get "/utils/where_am_i"

  get "/utils/system_stats"

  get "/utils/clear_completed_mdm_jobs"

  get "/utils/clear_old_completed_mdm_jobs"

  get "/utils/clear_old_mdm_jobs"

  get "/utils/find_missing_app_names"

  get "/utils/find_missing_bundle_ids"

  get "/utils/data_validation"

  get "/utils/truncate_sessions_table"

  get "/utils/truncate_delayed_jobs_table"

  get "/utils/fix_invalid_pool_memberships"

  get "/utils/remove_duplicate_device_enrollments"

  get "/utils/fix_licenses_of_legacy_orders_for_users"

  get "/utils/test_email_accepted"

  get "/utils/harvest_licenses"

  get "/utils/reset_table_sequences"

  get "/utils/fix_pool_apps"

  get "/utils/fix_license_costs"

  get "/utils/update_app_icons"

  get "/utils/index"

  get "/utils/check_order_data"

  get "/utils/check_order_data_fix"

Gemfile (in case it matters):

source 'http://rubygems.org'

gem 'rails', '~> 3.2.0'

gem 'nokogiri'

# Deploy with Capistrano

gem 'capistrano'

# Use Postgres DB
gem 'pg'

# Bundle gems for the local environment. Make sure to
# put test-only gems in this group so their generators
# and rake tasks are available in development mode:
group :development, :test do
  gem 'nifty-generators'
  gem 'ruby-debug19'
  gem 'looksee'
  #gem 'bullet'
  gem 'better_errors', '~> 1.0'
  gem 'binding_of_caller'
  #gem 'rack-mini-profiler'
end

gem 'bcrypt-ruby'#, '~> 3.1.5'
gem 'devise'
gem "devise_ldap_authenticatable"
gem 'haml'
gem 'haml-rails'
gem 'hpricot'
gem 'cancan'
gem 'seedbank'
gem 'mechanize'#, '~> 2.7'
gem 'roo'
gem 'rubyzip'
gem 'client_side_validations'
gem 'yaml_db'
gem 'sass'
gem 'will_paginate'
gem 'gibberish', '~> 1'
gem 'aws-s3'
gem 'delayed_job_active_record'#, '0.3.3'
gem 'daemons'
gem 'awesome_print'
gem "uuidtools"#, "~> 2.1.2"
gem "carrierwave"
gem "rmagick", :require => false
gem 'fog'
gem 'ruby-prof'
gem 'exception_notification'
gem 'grape'
gem 'yajl-ruby'
gem 'rqrcode'
gem 'pretender'
gem 'chronic'
gem 'unscoped_associations'
gem 'memoist'
gem 'oauth'
gem 'bcat'

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails'
  gem 'coffee-rails'
  gem 'uglifier'
end

gem 'jquery-rails'

Solution: I had a

def env
  ...
end

in my controller that never gave me a problem in rails 3.0. Renaming that fixed the issue.

1

There are 1 answers

0
NtroP On

My controller was overriding the env variable. I had:

def env
  ...
end

...in the controller that did't cause problems until upgrading to 3.2. Changing the name of the method fixed the problem. Thanks @boulder for pointing me in the right direction!