2012-08-12 123 views
34

我试图按照“Head First Rails”和第50页上的说明创建模型,但我无法使用rails命令创建模型。rails生成模型

当我在此提示符下键入:本地主机:〜$家

rails generate model ad name:string description:text price:decimal seller_id:integer email:string img_url:string 

我得到这个:

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$ 

我用Rails 3.2.8 -v和 红宝石1.9.3p125

+0

你有没有cd到你的rails项目目录? – PeterWong 2012-08-12 18:10:36

+0

我认为这可能是问题所在。我没有项目目录。本书是为rails 2.1编写的在本教程中,我使用'rails mebay'创建目录,但该命令在Rails 3.2.8中不起作用。我不应该使用'rails new mebay',因为我应该手动创建模型和控制器。我认为这本书缺少一个步骤。 – Livi17 2012-08-12 18:26:21

回答

63

代码是好的,但你在错误的目录中。您必须在rails项目目录中运行这些命令。

方式,一般从头那里是:

$ 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 
+1

查看本课程旨在向您展示如何手动创建模型和控制器。它说使用“​​rails new mebay”创建了一个脚手架应用程序,但脚手架应用程序提供了比我们需要的更多。所以我们应该首先使用“rails generate model ad blah:string”手动创建模型。除了它没有告诉我们如何首先创建应用程序目录。我认为这本书缺少一个步骤。 – Livi17 2012-08-12 18:33:29

+0

我会尝试上面的建议 – Livi17 2012-08-12 18:34:26

+0

命令'rails new mebay'不会生成脚手架应用程序。它只生成rails-app的skaleton。没有这些,你就无法开始编写你的应用程序。 – 2012-08-12 18:35:14

1

对我发生了什么事,我产生与轨道的新轨道新chapter_2 但RVM --default有轨4.0.2宝石的应用程序,但我chapter_2项目中使用了新的 宝石与轨道3.2.16。

所以,当我跑

rails generate scaffold User name:string email:string 

控制台显示

Usage: 
    rails new APP_PATH [options] 

所以我固定RVM并与轨道3.2.16宝石的宝石,然后生成应用程序再次 后来我执行

rails generate scaffold User name:string email:string 

和它的工作

7

错误显示您要么没有创建rails项目,要么你不在rails项目目录中。

假设您正在开发myapp项目。您必须在命令行上移至该项目目录,然后生成模型。以下是您可以参考的一些步骤。

例子:假设你没有创建Rails应用程序尚未:

​​

现在生成您的命令行模式。

$> rails generate model your_model_name