Ruby on rails rails生成模型

Ruby on rails rails生成模型,ruby-on-rails,ruby-on-rails-3.2,Ruby On Rails,Ruby On Rails 3.2,我试图按照《头优先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]

我试图按照《头优先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-V3.2.8
Ruby 1.9.3p125

代码还可以,但您的目录不正确。您必须在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

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

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

尝试查找Rails 3教程,有很多变化,因为2.1指南()是一个很好的起点。

对我来说,发生的事情是我用Rails新Rails新章节2生成了应用程序 但是RVM-default有rails 4.0.2 gem,但是我的chapter_2项目使用了一个新的 带导轨的gemest 3.2.16

所以当我跑的时候

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

它成功了

错误显示您尚未创建rails项目,或者您不在rails项目目录中

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

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

$> rails new myapp
$> cd myapp
现在从命令行生成模型

$> rails generate model your_model_name 

你把你的rails项目目录放进cd了吗?我想这可能是问题所在。我没有项目总监。本书是为rails版本2.1编写的。在本教程中,我打算使用“rails mebay”创建一个目录,但该命令在rails 3.2.8中不起作用。我不应该使用“rails new mebay”,因为我应该手动创建模型和控制器。我认为这本书中缺少了一个步骤。请参阅本课程旨在向您展示如何手动创建模型和控制器。它说,使用“rails new mebay”创建了一个脚手架应用程序,但脚手架应用程序提供的内容超出了我们的需要。因此,我们应该首先使用“rails生成模型ad blah:string”手动创建模型。只是它没有告诉我们如何首先创建应用程序目录。我认为这本书中缺少了一个步骤。我将尝试你的建议。命令
rails new-mebay
不会生成scaffold应用程序。它只生成rails应用程序的skaleton。没有这些,你就不能开始写你的应用程序了。我的错误。我按照你的步骤做了,这似乎奏效了。这本书仍然遗漏了第一步“rails新项目名称”。谢谢你的帮助!此命令的缩写版本是
rails g model ModelName column\u name:string