Ruby-on-rails rails 生成模型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11924526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
rails generate model
提问by Livi17
I'm trying to follow instructions from the book "Head First Rails" and on page 50 it says to create a model but I am unable to create a model using the rails command.
我正在尝试按照“Head First Rails”一书中的说明进行操作,并且在第 50 页上说要创建模型,但我无法使用 rails 命令创建模型。
When I type this at this prompt: localhost:~ home$
当我在这个提示符下输入时: localhost:~ home$
rails generate model ad name:string description:text price:decimal seller_id:integer email:string img_url:string
I get this:
我明白了:
Usage:
rails new APP_PATH [options]
Options:
-r, [--ruby=PATH] # Path to the Ruby binary of your choice
# Default: /Users/home/.rvm/rubies/ruby-1.9.3-p125/bin/ruby
-b, [--builder=BUILDER] # Path to a application builder (can be a filesystem path or URL)
-m, [--template=TEMPLATE] # Path to an application template (can be a filesystem path or URL)
[--skip-gemfile] # Don't create a Gemfile
[--skip-bundle] # Don't run bundle install
-G, [--skip-git] # Skip Git ignores and keeps
-O, [--skip-active-record] # Skip Active Record files
-S, [--skip-sprockets] # Skip Sprockets files
-d, [--database=DATABASE] # Preconfigure for selected database (options: mysql/oracle/postgresql/sqlite3/frontbase/ibm_db/sqlserver/jdbcmysql/jdbcsqlite3/jdbcpostgresql/jdbc)
# Default: sqlite3
-j, [--javascript=JAVASCRIPT] # Preconfigure for selected JavaScript library
# Default: jquery
-J, [--skip-javascript] # Skip JavaScript files
[--dev] # Setup the application with Gemfile pointing to your Rails checkout
[--edge] # Setup the application with Gemfile pointing to Rails repository
-T, [--skip-test-unit] # Skip Test::Unit files
[--old-style-hash] # Force using old style hash (:foo => 'bar') on Ruby >= 1.9
Runtime options:
-f, [--force] # Overwrite files that already exist
-p, [--pretend] # Run but do not make any changes
-q, [--quiet] # Suppress status output
-s, [--skip] # Skip files that already exist
Rails options:
-h, [--help] # Show this help message and quit
-v, [--version] # Show Rails version number and quit
Description:
The 'rails new' command creates a new Rails application with a default
directory structure and configuration at the path you specify.
You can specify extra command-line arguments to be used every time
'rails new' runs in the .railsrc configuration file in your home directory.
Note that the arguments specified in the .railsrc file don't affect the
defaults values shown above in this help message.
Example:
rails new ~/Code/Ruby/weblog
This generates a skeletal Rails installation in ~/Code/Ruby/weblog.
See the README in the newly created application to get going.
localhost:~ home$
I am using Rails -v 3.2.8 and Ruby 1.9.3p125
我正在使用 Rails -v 3.2.8 和 Ruby 1.9.3p125
回答by Marcel Hebing
The code is okay but you are in the wrong directory. You must run these commands inside your rails project-directory.
代码没问题,但你在错误的目录中。您必须在 rails 项目目录中运行这些命令。
The normal way to get there from scratch is:
从头开始的正常方法是:
$ rails new PROJECT_NAME
$ cd PROJECT_NAME
$ rails generate model ad \
name:string \
description:text \
price:decimal \
seller_id:integer \
email:string img_url:string
回答by Prabhakar Undurthi
The error shows you either didn't create the rails project yet or you're not in the rails project directory.
该错误表明您尚未创建 rails 项目,或者您不在 rails 项目目录中。
Suppose if you're working on myapp project. You've to move to that project directory on your command line and then generate the model. Here are some steps you can refer.
假设您正在处理 myapp 项目。您必须在命令行上移动到该项目目录,然后生成模型。以下是一些您可以参考的步骤。
Example: Assuming you didn't create the Rails app yet:
示例:假设您还没有创建 Rails 应用程序:
$> rails new myapp
$> cd myapp
Now generate the model from your commandline.
现在从命令行生成模型。
$> rails generate model your_model_name
回答by dimuch
You need to create new rails application first. Run
您需要先创建新的 rails 应用程序。跑
rails new mebay
cd mebay
bundle install
rails generate model ...
And try to find Rails 3 tutorial, there are a lot of changes since 2.1 Guides (http://guides.rubyonrails.org/getting_started.html) are good start point.
并尝试查找 Rails 3 教程,因为 2.1 指南(http://guides.rubyonrails.org/getting_started.html)是很好的起点,因此有很多变化。
回答by JPRLCol
For me what happened was that I generated the app with rails new rails new chapter_2 but the RVM --default had rails 4.0.2 gem, but my chapter_2 project use a new gemset with rails 3.2.16.
对我来说,发生的事情是我用 rails new rails new Chapter_2 生成了应用程序,但 RVM --default 有 rails 4.0.2 gem,但我的 Chapter_2 项目使用了带有 rails 3.2.16 的新 gemset。
So when I ran
所以当我跑
rails generate scaffold User name:string email:string
the console showed
控制台显示
Usage:
rails new APP_PATH [options]
So I fixed the RVM and the gemset with the rails 3.2.16 gem , and then generated the app again then I executed
所以我用 rails 3.2.16 gem 修复了 RVM 和 gemset,然后再次生成应用程序,然后我执行
rails generate scaffold User name:string email:string
and it worked
它起作用了

