【问题标题】:ActionController::RoutingError (Couldn't find..., expected it to be defined in...)ActionController::RoutingError(找不到...,希望它定义在...中)
【发布时间】:2017-08-13 16:24:08
【问题描述】:

所以...第一次向 StackOverflow 提问...

按照 Rails 指南和 RailsApps 项目,我将现有的 Rails 4.2.5 应用程序(使用 Ruby 2.2.4)转换为 Rails 5.1.3 应用程序(使用 Ruby 2.4.1)。

启动开发服务器会产生:

Taruns-iMac:Play_Ball tarunchattoraj$ rails s
=> Booting Puma
=> Rails 5.1.3 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.9.1 (ruby 2.4.1-p111), codename: Private Caller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop

这是我所期望的。

但是,导航到 localhost:3000 会产生 ActionController 错误:

Started GET "/" for 127.0.0.1 at 2017-08-13 11:05:38 -0400
   (0.2ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
  
ActionController::RoutingError (Couldn't find Api::KeepScoresHelper, expected it to be defined in helpers/api/keep_scores_helper.rb):
  
app/controllers/application_controller.rb:1:in `<top (required)>'
app/controllers/welcome_controller.rb:1:in `<top (required)>'

我希望看到我的应用程序在页面上正常运行。

我的问题:

是什么导致了这个 ActionController::RoutingError (Couldn't find...) 以及如何纠正它?

这个question 似乎是正确的。我尝试了“触摸”所有帮助文件的解决方案,但无济于事。

当我从 Rails 4.2.5 转换到 5.1.3 时,我运行了 $ rails app:update 并覆盖了大多数文件——那些我没有特定于我的应用程序的代码,但我选择不覆盖 config/路线.rb。

config/routes.rb:

Rails.application.routes.draw do
  resources :alerts
  get 'games/decide_game_view' => 'games#decide_game_view', as: :decide_game_view
  resources :games
  resources :game_hitting_stats
  resources :game_pitching_stats
  resources :locations
  resources :players
  get 'welcome/index'

  get 'welcome/about'



  resources :users do
    resources :players
  end

  resources :sessions, only: [:new, :create, :destroy]
  resources :teams do
    resources :notes
  end

  # namespace :api, defaults: {format: :http} do
  namespace :api do
    match '/texts', to: 'texts#preflight', via: [:options]
    resources :texts, only: [:create]

    match '/keepscore/teams', to: 'keep_scores#preflight', via: [:options]
    get 'keepscore/teams' => 'keep_scores#get_teams', as: :get_teams

    match '/keepscore/roster', to: 'keep_scores#preflight', via: [:options]
    get 'keepscore/roster' => 'keep_scores#get_roster', as: :get_roster

    match '/keepscore/post_game_stats', to: 'keep_scores#preflight', via: [:options]
    post 'keepscore/post_game_stats' => 'keep_scores#post_game_stats', as: :post_game_stats

  end
  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"
  # root 'welcome#index'

  # Example of regular route:
  #   get 'products/:id' => 'catalog#view'

  # Example of named route that can be invoked with purchase_url(id: product.id)
  #   get 'products/:id/purchase' => 'catalog#purchase', as: :purchase

  # Example resource route (maps HTTP verbs to controller actions automatically):
  #   resources :products

  # Example resource route with options:
  #   resources :products do
  #     member do
  #       get 'short'
  #       post 'toggle'
  #     end
  #
  #     collection do
  #       get 'sold'
  #     end
  #   end

  # Example resource route with sub-resources:
  #   resources :products do
  #     resources :comments, :sales
  #     resource :seller
  #   end

  # Example resource route with more complex sub-resources:
  #   resources :products do
  #     resources :comments
  #     resources :sales do
  #       get 'recent', on: :collection
  #     end
  #   end

  # Example resource route with concerns:
  #   concern :toggleable do
  #     post 'toggle'
  #   end
  #   resources :posts, concerns: :toggleable
  #   resources :photos, concerns: :toggleable

  # Example resource route within a namespace:
  #   namespace :admin do
  #     # Directs /admin/products/* to Admin::ProductsController
  #     # (app/controllers/admin/products_controller.rb)
  #     resources :products
  #   end
  root 'welcome#index'
end

ActionController 似乎告诉我它找不到 KeepScoresHelper 模块,并且它在 helpers/api/keep_scores_helper.rb 中查找。但该文件存在 请参阅Pic of App Controller and Helper Tree Structure

helpers/api/keep_scores_helper.rb的内容:

module API::KeepScoresHelper
end

宝石文件:

source 'https://rubygems.org'

ruby "2.4.1"
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '5.1.3'
gem 'puma'

gem 'pundit'
gem 'bcrypt'

gem 'active_model_serializers', '~> 0.10.0'

# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2.2'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
gem 'jquery-turbolinks'
gem 'bootstrap-sass'
gem 'figaro'
gem 'pry'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :production do
  gem 'pg'
  gem 'rails_12factor'
end

group :test do
  gem 'pundit-matchers', '~>1.0.1'
end

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'listen'
  gem 'byebug'
  gem 'web-console', '~> 3.5.1'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'rspec-rails'
  gem 'shoulda'
  gem 'faker'
  gem 'factory_girl_rails'
  gem 'rails-controller-testing'
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> in views

  gem 'sqlite3'


end

当然非常感谢任何帮助...

【问题讨论】:

    标签: actioncontroller ruby-on-rails-5.1


    【解决方案1】:

    在 config/environments/development.rb 中,设置:

    config.action_controller.include_all_helpers = false

    这会将 Helpers 返回到 Rails 5 之前的行为。根据 RoR API,对于 Rails 5.1.3,“默认情况下,每个控制器都将包含所有帮助程序。”在 Rails 5 之前,控制器将包含一个与其名称匹配的助手。进行上述指定的设置将返回到 Pre-Rails 5 行为。

    【讨论】:

      【解决方案2】:

      添加以帮助其他偶然发现此问题的人。

      初步故障排除

      升级您在问题中提到的相同版本时,我遇到了同样的问题。我确实先从 4.2 升级到 5.0,然后再从那里升级到 5.1.3。

      第一次升级运行良好,但我开始收到相同的错误消息(例如,uninitialized constant ActionController::RedirectBackError ... 关于无法找到帮助程序,但这仅在我跳转到 rails 5.1 时发生。

      我尝试了github issue 中概述的许多其他修复,因为它似乎是相同的错误,但没有任何效果,而且我的路径似乎都是正确的(路径或大小写没有差异)。

      我的修复

      我最终删除了所有帮助程序并收到一个新错误,导致我在我的应用中找到导致问题的代码。

      # app/controllers/application_controller.rb
      # Catch exceptions if :back is not set throughout the app. This is a fallback redirect if request.referer is not set.
      
      rescue_from ActionController::RedirectBackError do |exception|
        redirect_to key_activities_ministries_activities_path, alert: exception.message
      end
      

      该代码导致路由错误:uninitialized constant ActionController::RedirectBackError。但是这个错误只有在我删除所有助手时才出现。 我删除了这段代码,把我的助手放回去,然后重新启动了我的服务器,一切都按预期工作了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-07
        • 2012-03-23
        • 1970-01-01
        • 2015-07-17
        相关资源
        最近更新 更多