57

我正在尝试按照“Head First Rails”一书中的说明进行操作,第 50 页上说要创建一个模型,但我无法使用 rails 命令创建一个模型。

当我在这个提示符下输入:localhost:~ home$

 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 -v 3.2.8 和 Ruby 1.9.3p125

4

4 回答 4

92

代码没问题,但您在错误的目录中。您必须在您的 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
于 2012-08-12T18:28:32.577 回答
10

该错误表明您尚未创建 rails 项目,或者您不在 rails 项目目录中。

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

示例:假设您尚未创建 Rails 应用程序:

$> rails new myapp
$> cd myapp

现在从命令行生成模型。

$> rails generate model your_model_name 
于 2014-12-12T17:03:32.743 回答
3

对我来说,发生的事情是我使用 rails new rails new chapter_2 生成了应用程序,但 RVM --default 具有 rails 4.0.2 gem,但我的 chapter_2 项目使用带有 rails 3.2.16 的新 gemset。

所以当我跑

rails generate scaffold User name:string email:string

控制台显示

Usage:
   rails new APP_PATH [options]

所以我用 rails 3.2.16 gem 修复了 RVM 和 gemset,然后再次生成应用程序然后我执行

 rails generate scaffold User name:string email:string

它奏效了

于 2014-02-10T16:07:50.503 回答
2

您需要先创建新的 Rails 应用程序。跑

rails new mebay
cd mebay
bundle install
rails generate model ...

并尝试查找 Rails 3 教程,因为 2.1 指南 ( http://guides.rubyonrails.org/getting_started.html ) 是一个很好的起点,所以有很多变化。

于 2012-08-12T18:32:39.513 回答