轨生成模式(rails generate model)

2019-06-26 23:57发布

我想遵循本书“头第一轨道”和第50页上的说明,它说创建一个模型,但我无法创建使用轨道命令的典范。

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

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

Answer 1:

该代码是好的,但你是在错误的目录。 您必须在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


Answer 2:

错误说明你要么不创建Rails项目又或者你在Rails项目目录不是。

假设如果你在MyApp项目工作。 您对移动到该项目目录的命令行,然后生成模型。 这里有一些步骤,你可以参考。

例如:假设您没有创建Rails应用程序尚未:

$> rails new myapp
$> cd myapp

现在,通过您的命令行模式。

$> rails generate model your_model_name 


Answer 3:

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

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

并试图找到Rails的3教程,有很多因为2.1指南(变化http://guides.rubyonrails.org/getting_started.html )都是很好的起点。



Answer 4:

对我来说,实际情况是,我产生与轨道的新轨道新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

和它的工作



文章来源: rails generate model