ruby-on-rails 如何配置没有数据库的Ruby on Rails?

8nuwlpux  于 2023-03-20  发布在  Ruby
关注(0)|答案(6)|浏览(165)

对于目前不需要数据库的小型网站项目,使用RubyonRails会很方便,我知道我可以在MySQL中创建一个空数据库,然后从那里开始,但是有人知道在没有数据库的情况下运行Rails的更好方法吗?
谢谢

gupuwyp2

gupuwyp21#

对于导轨3导轨4
使用-O(大写“O”)或--skip-activerecord选项生成不带数据库的应用程序。
rails new myApp -O

rails new myApp --skip-activerecord
此答案从此处重新共享
对于导轨5
使用--skip-active-record选项生成不带数据库的应用程序

请注意与之前的Rails版本相反的额外连字符“-”

rails new myApp --skip-active-record

jyztefdp

jyztefdp2#

对于新项目,使用简单的rails new your_app --skip-active-record就可以了,但是对于现有的Rails 4-7项目,在config/application.rb文件中有以下行:

require 'rails/all' # or `require "rails"' in newer versions

(As参考线路正在加载this file
因此,您必须按如下方式分别加载每个库,而不是加载ALL:

# active_record is what we're not going to use it, so add it comment in case 
# at some point you enable it back again
# require "active_record/railtie" 

# This is not loaded in rails/all but inside active_record so add it if
# you want your models work as expected
require "active_model/railtie" 
# And now the rest
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "active_job/railtie" # Only for Rails >= 4.2
require "action_cable/engine" # Only for Rails >= 5.0
require "sprockets/railtie" # Deprecated for Rails >= 7, so add it only if you're using it
require "rails/test_unit/railtie"

# All these depend on active_record, so they should be excluded also
# require "action_text/engine" # Only for Rails >= 6.0
# require "action_mailbox/engine" # Only for Rails >= 6.0
# require "active_storage/engine" # Only for Rails >= 5.2

请留意注解,了解关于您的Rails版本要加载哪些内容。
还要检查以下文件(如果有)并注解以下行:

# package.json
"@rails/activestorage": "^6.0.0",

# app/javascript/packs/application.js
require("@rails/activestorage").start()

# bin/setup
system! 'bin/rails db:prepare'

# config/environments/development.rb
config.active_storage.service = :local # For Rails >= 5.2
config.active_record.migration_error = :page_load
config.active_record.verbose_query_logs = true

# config/environments/test.rb
config.active_storage.service = :test # For Rails >= 5.2

# config/environments/production.rb
config.active_storage.service = :local # For Rails >= 5.2
config.active_record.dump_schema_after_migration = false

# spec/rails_helper.rb
ActiveRecord::Migration.maintain_test_schema!

# test/test_helper.rb
fixtures :all # In case you're using fixtures

# Only for Rails >= 5.0
#config/initializers/new_framework_defaults.rb
Rails.application.config.active_record.belongs_to_required_by_default = true

还要删除模型文件中对ActiveRecord::Base的任何引用(如果适用,也可以直接删除这些文件),例如,自动生成的app/models/application_record.rb文件。

hujrc8aj

hujrc8aj3#

取消注解environment.rb文件中的此行:

config.frameworks -= [ :active_record, :active_resource, :action_mailer]
sh7euo9m

sh7euo9m4#

在Rails 4中,启动新项目时,可以使用-O或--skip-active-record

rails new my_project -O
rails new my_project --skip-active-record

如果您已经创建了一个项目,则需要添加注解

require "active_record/railtie"

从配置/应用程序.rb和

config.active_record.migration_error = :page_load

来自配置/环境/开发.rb

ie3xauqp

ie3xauqp5#

如果你不需要数据库,那么你可能不需要大量的Rails,你可能需要一个更小的、更可定制的框架。
Sinatra是一个非常小的框架,非常适合提供基本的静态页面。
但是如果您坚持使用Rails,那么有一篇文章将向您展示如何使用Rails herehere

odopli94

odopli946#

为了支持Rails 6 rc 1和activerecord-nulldb-adapter gem,我们需要一个猴子补丁
配置/初始化程序/null_db_adapter_monkey_patches.rb

module ActiveRecord
  module ConnectionAdapters
    class NullDBAdapter < ActiveRecord::ConnectionAdapters::AbstractAdapter
      def new_table_definition(table_name = nil, is_temporary = nil)
        TableDefinition.new(table_name, is_temporary)
      end
    end
  end
end

相关问题