ActionController :: RoutingError(找不到......,期望它在...中定义)

时间:2017-08-13 16:24:09

标签: actioncontroller ruby-on-rails-5.1

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

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

启动开发服务器会产生:

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(找不到...)以及如何纠正它?

question似乎很明显。我尝试了“触摸”所有帮助文件的解决方案,但无济于事。

当我从Rails 4.2.5转换为5.1.3时,我运行$ rails app:update并覆盖了大多数文件 - 那些我没有特定于我的应用程序的代码的文件,但我选择不覆盖配置/ routes.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

的Gemfile:

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

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

2 个答案:

答案 0 :(得分:3)

希望帮助那些偶然发现这一点的人。

初始故障排除

升级您在问题中提到的相同版本时,我遇到了同样的问题。我首先从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。但是这个错误只出现在我删除所有助手的时候。 我删除了此代码,重新启用了帮助程序,然后重新启动了服务器,一切都按预期工作。

答案 1 :(得分:2)

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

config.action_controller.include_all_helpers = false

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