如何解决'block in draw' 轨道 6 条路线
在移动到 rails 6 后,我试图将我的路线拆分为不同的文件,我已经检查了文档并完成了以下操作:
Rails.application.routes.draw do
root 'summary#index'
draw(:general)
end
在`api_schema_controller 中
module Api
module V1
class ApiSchemaController < ApplicationController
CONTROLLER_NAMES = %w(SalesOrders VatRates VatTypes Companies ExchangeRates Auth Products BankAccounts Locations Customers
SalesInvoices Receipts Accounts BudgetModels)
require_relative '../../../../lib/internal_apI/Open_api_builder'
def self.build_route_for_action(routing_object,controller_name,controller_class,action_name,action_conf)
path = ApiSchemaController.action_path(controller_class,action_conf,true)
verb = action_conf[:verb].present? ? action_conf[:verb].to_s : 'get'
routing_object.send(verb,path,action: action_name,controller: controller_name.underscore)
end
def self.build_routes(routing_thing)
unless ActiveRecord::Migrator.needs_migration?
self::CONTROLLER_NAMES.each do |controller_name|
module_root = self.module_root
controller_class = (module_root + '::' + controller_name + 'Controller').constantize
controller_class::ACTIONS.each do |action_name,action_conf|
self.build_route_for_action(routing_thing,action_conf.deep_dup)
end
end
end
end
def self.module_root
self.to_s.gsub(/::[^:]*$/,'') # not super sure about this reg ex,maybe splitting would be better
end
def open_api
controller_names = self.class::CONTROLLER_NAMES
if params[:only_controllers].present?
only = params[:only_controllers].split(',')
controller_names = only.select { |c| c.in?(controller_names) }
end
module_root = self.class.module_root
schema_builder = OpenApiBuilder.new
controller_names.each do |controller_name|
schema_builder.add_paths_from_controller_class(
(module_root + '::' + controller_name + 'Controller').constantize
)
end
render json: schema_builder.to_json
# render text: schema_builder.to_yaml,content_type: 'text/yaml'
end
def get_node_test_result
dir = File.expand_path('../../../../../node',__FILE__)
render json: `node #{dir}/index.js`
end
protected
def self.action_path(controller_class,for_route = false)
path = '/' + controller_class.controller_name.gsub('_','-')
path += action_conf[:path] unless action_conf[:path].blank?
path = path.gsub(/{(.*?)}/,':\1') if for_route
path
end
end
end
end
在 config/routes/general.rb
我有以下内容
# api routes
namespace :api do
namespace :v1 do
get 'ping',action: :ping,controller: 'summary'
get 'version',action: :version,controller: 'summary'
delete 'sign_out',action: :sign_out,controller: 'authenticate'
Api::V1::ApiSchemaController.build_routes(self)
end
end
但是,我在启动服务器时收到以下信息
Error:[rake --prereqs] /home/ben/code/BusinessCloudEssential/app/controllers/api/v1/api_schema_controller.rb:17:in `build_routes': undefined method `needs_migration?' for ActiveRecord::Migrator:Class (NoMethodError)
from /home/ben/code/app/config/routes/general.rb:8:in `block (2 levels) in draw'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/actionpack-6.1.3.2/lib/action_dispatch/routing/mapper.rb:970:in `block (2 levels) in namespace'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/actionpack-6.1.3.2/lib/action_dispatch/routing/mapper.rb:901:in `scope'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/actionpack-6.1.3.2/lib/action_dispatch/routing/mapper.rb:970:in `block in namespace'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/actionpack-6.1.3.2/lib/action_dispatch/routing/mapper.rb:1886:in `path_scope'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/actionpack-6.1.3.2/lib/action_dispatch/routing/mapper.rb:969:in `namespace'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/actionpack-6.1.3.2/lib/action_dispatch/routing/mapper.rb:1596:in `namespace'
from /home/ben/code/BusinessCloudEssential/config/routes/general.rb:3:in `block in draw'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/actionpack-6.1.3.2/lib/action_dispatch/routing/mapper.rb:970:in `block (2 levels) in namespace'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/actionpack-6.1.3.2/lib/action_dispatch/routing/mapper.rb:901:in `scope'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/actionpack-6.1.3.2/lib/action_dispatch/routing/mapper.rb:970:in `block in namespace'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/actionpack-6.1.3.2/lib/action_dispatch/routing/mapper.rb:1886:in `path_scope'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/actionpack-6.1.3.2/lib/action_dispatch/routing/mapper.rb:969:in `namespace'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/actionpack-6.1.3.2/lib/action_dispatch/routing/mapper.rb:1596:in `namespace'
from /home/ben/code/BusinessCloudEssential/config/routes/general.rb:2:in `draw'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/actionpack-6.1.3.2/lib/action_dispatch/routing/mapper.rb:1624:in `instance_eval'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/actionpack-6.1.3.2/lib/action_dispatch/routing/mapper.rb:1624:in `draw'
from /home/ben/code/app/config/routes.rb:9:in `block in <top (required)>'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/actionpack-6.1.3.2/lib/action_dispatch/routing/route_set.rb:427:in `instance_exec'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/actionpack-6.1.3.2/lib/action_dispatch/routing/route_set.rb:427:in `eval_block'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/actionpack-6.1.3.2/lib/action_dispatch/routing/route_set.rb:409:in `draw'
from /home/ben/code/app/config/routes.rb:1:in `<top (required)>'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.7.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:59:in `load'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.7.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:59:in `load'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-6.1.3.2/lib/rails/application/routes_reloader.rb:47:in `block in load_paths'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-6.1.3.2/lib/rails/application/routes_reloader.rb:47:in `each'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-6.1.3.2/lib/rails/application/routes_reloader.rb:47:in `load_paths'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-6.1.3.2/lib/rails/application/routes_reloader.rb:21:in `reload!'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-6.1.3.2/lib/rails/application/routes_reloader.rb:35:in `block in updater'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-6.1.3.2/lib/active_support/file_update_checker.rb:83:in `execute'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-6.1.3.2/lib/rails/application/routes_reloader.rb:10:in `execute'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-6.1.3.2/lib/rails/application/finisher.rb:195:in `block in <module:Finisher>'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-6.1.3.2/lib/rails/initializable.rb:32:in `instance_exec'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-6.1.3.2/lib/rails/initializable.rb:32:in `run'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-6.1.3.2/lib/rails/initializable.rb:61:in `block in run_initializers'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/2.5.0/tsort.rb:228:in `block in tsort_each'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/2.5.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/2.5.0/tsort.rb:431:in `each_strongly_connected_component_from'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/2.5.0/tsort.rb:349:in `block in each_strongly_connected_component'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/2.5.0/tsort.rb:347:in `each'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/2.5.0/tsort.rb:347:in `call'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/2.5.0/tsort.rb:347:in `each_strongly_connected_component'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/2.5.0/tsort.rb:226:in `tsort_each'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/2.5.0/tsort.rb:205:in `tsort_each'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-6.1.3.2/lib/rails/initializable.rb:60:in `run_initializers'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-6.1.3.2/lib/rails/application.rb:384:in `initialize!'
from /home/ben/code/app/config/environment.rb:5:in `<top (required)>'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.7.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.7.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.7.5/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.7.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bootsnap-1.7.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/zeitwerk-2.4.2/lib/zeitwerk/kernel.rb:34:in `require'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-6.1.3.2/lib/active_support/dependencies.rb:332:in `block in require'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-6.1.3.2/lib/active_support/dependencies.rb:299:in `load_dependency'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-6.1.3.2/lib/active_support/dependencies.rb:332:in `require'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.1.1/lib/spring/application.rb:106:in `preload'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.1.1/lib/spring/application.rb:157:in `serve'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.1.1/lib/spring/application.rb:145:in `block in run'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.1.1/lib/spring/application.rb:139:in `loop'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.1.1/lib/spring/application.rb:139:in `run'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.1.1/lib/spring/application/boot.rb:19:in `<top (required)>'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:85:in `require'
from /home/ben/.rbenv/versions/2.5.0/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:85:in `require'
from -e:1:in `<main>'
我不知道错误是什么,因为我已经遵循了文档,但我这似乎是 api 生成路由的方式,而不是我的代码,我只是不确定重构它的最佳方法。
解决方法
错误 undefined method 'needs_migration?' for ActiveRecord::Migrator:Class (NoMethodError)
被抛出,因为方法 needs_migration?
属于 ActiveRecord::MigrationContext
而不是 ActiveRecord::Migrator
试试这个
# api_schema_controller
# ...
def self.build_routes(routing_thing)
unless ActiveRecord::Base.connection.migration_context.needs_migration?
#...
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。