【问题标题】:rails g model or rails g scaffold doesn't create modelrails g 模型或 rails g 脚手架不创建模型
【发布时间】:2020-07-30 09:37:04
【问题描述】:

我正在开始使用 Ruby,并正在开发简单的应用程序以获取基础知识。

我使用 --skip-active-record 标志创建应用程序。现在我需要一个模型,rails g model Articles title:string text:string or rails g scaffold Articles title:string text:string 都不会生成模型(注意:scaffold 命令会创建除模型之外的所有内容)。

环境:

  • Win 7 x64
  • ruby 2.7.1p83(2020-03-31 修订版 a0c7c23c9c)[x64-mingw32]
  • Rails 6.0.3.2

宝石文件

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.7.1'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.0.3', '>= 6.0.3.2'
# Use Puma as the app server
gem 'puma', '~> 4.1'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false

# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
# gem 'rack-cors'

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

gem 'ruby_cowsay'

gem 'jbuilder'

gem 'sqlite3', git: "https://github.com/larskanis/sqlite3-ruby", branch: "add-gemspec"

gem 'activerecord', '~> 6.0', '>= 6.0.3.2'

您对问题出在哪里有想法吗?

【问题讨论】:

  • 如果您不告诉我们错误是什么,我们将无法帮助您:) 请在尝试您的命令后粘贴您的终端输出
  • 为空,没有错误。第一个命令执行并打印(并创建)任何内容。 Scaffold 命令创建(并在控制台中打印)除模型之外的所有预期内容
  • 如果在查看问题后您没有发现任何问题,我想请教一下,请按照我的步骤创建模型。如果创建了模型,请分享您的 gemfile 和其他环境条件
  • 有点奇怪:你的rails g model 通常会创建迁移来创建表,模型总是从ActiveRecord::Base 派生的,因为你没有使用活动记录,rails 是如何知道你想要的存储/创建列和模型?您对此有何期待?
  • 我的意图是向现有应用程序添加活动记录,我更喜欢使用 sqlite3 作为存储。您可能会注意到上面 gemfile 中的 activerecord gem。它应该回答评论中的第一个问题。我对上述命令的期望是创建一个模型。

标签: ruby-on-rails ruby rails-activerecord


【解决方案1】:

据我所知,您已经使用了--skip-active-record,您是在通知应用程序您不会使用模型。出于这个原因,它实际上是在跳过模型文件夹。我不确定您是否 100% 确定您的动机。但如果你想在这种情况下启用它,你可以执行以下操作。

1. Add database related gem in Gemfile ex: gem 'mysql2'
2. In config/application.rb uncomment the line require "active_record/railtie"
3. Add database.yml

    

【讨论】:

  • 谢谢,我使用了 sqlite3 并遇到了来自 activesupport 依赖项的问题“无法加载此类文件 -- sqlite3/sqlite3_native”
  • 嗨@JoeDow 抱歉回复晚了。你有没有设法修复它。我不确定 sqlite3_native 问题。您可以查看以下链接,我想这会对您有所帮助。 stackoverflow.com/questions/17643897/…
  • 感谢您的跟进。存在一些依赖性问题
猜你喜欢
  • 2012-04-06
  • 2021-09-18
  • 1970-01-01
  • 2011-10-29
  • 2013-07-18
  • 2011-10-09
  • 1970-01-01
  • 2013-09-03
  • 1970-01-01
相关资源
最近更新 更多