gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:36:07 28 4
gpt4 key购买 nike

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

我按照 Rails Guides 和 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(无法找到...)以及如何纠正它?

这个 question 似乎是切中要害的。我尝试了“触摸”所有帮助文件的解决方案,但没有成功。

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

配置/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

gem 文件:

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

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

最佳答案

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

config.action_controller.include_all_helpers = false

这将使 Helpers 恢复到 Rails 5 之前的行为。每RoR API ,对于 Rails 5.1.3,“默认情况下,每个 Controller 将包含所有助手。”在 Rails 5 之前, Controller 将包含一个与其名称匹配的助手。进行上述指定的设置将返回到 Pre-Rails 5 行为。

关于ActionController::RoutingError(找不到...,期望它在...中定义),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45662462/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com