簡體   English   中英

rails生成模型

[英]rails generate model

我試圖按照“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

代碼沒問題,但你在錯誤的目錄中。 您必須在rails project-directory中運行這些命令。

從頭開始的正常方法是:

$ 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項目目錄中。

假設您正在開發myapp項目。 您將在命令行上移動到該項目目錄,然后生成模型。 以下是您可以參考的一些步驟。

示例:假設您尚未創建Rails應用程序:

$> rails new myapp
$> cd myapp

現在從命令行生成模型。

$> rails generate model your_model_name 

您需要先創建新的rails應用程序。

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

並嘗試找到Rails 3教程,自2.1指南( http://guides.rubyonrails.org/getting_started.html )以來有很多變化是很好的起點。

對我來說,發生的事情是我使用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

它起作用了

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM