未定义的方法`[]' for false:link_to中的FalseClass

时间:2015-06-24 17:59:43

标签: ruby-on-rails ruby ruby-on-rails-3.2 link-to

我将我的rails 3.0应用程序升级到3.1(然后3.2希望它能解决这个问题 - 它没有做到)。自升级以来,如果我添加:controller:action

,我的视图中的link_to功能将失败
%li
    =link_to "foo",:controller => "utils", :action =>  'my_controller_action'

这是我得到的堆栈跟踪:

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'
...

路线和方法在那里(它在3.0中运行良好)。如果我对URL进行硬编码,它就可以工作。

当我调试代码时,我得到: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

当我检查if _routes.equal?(env["action_dispatch.routes"])时,env false 而不是通常的路线数组......

有什么想法吗?

更新了所需信息:

 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

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(如果重要):

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'

解决方案:我有一个

def env
  ...
end

在我的控制器中,从未在rails 3.0中给我一个问题。重命名以解决问题。

1 个答案:

答案 0 :(得分:0)

我的控制器覆盖了env变量。我有:

def env
  ...
end

...在升级到3.2之前不会导致问题的控制器中。更改方法的名称修复了问题。谢谢@boulder指出我正确的方向!