• rails generate model/resource/scaffold的区别


    If you’re just learning Ruby on Rails, you may be confused as to when to generate individual models, resources or scaffolding, and what files are created by each command.

    Say you want to generate a Test model with a name.  You could either generate the model individually, generate resources, or generate scaffolding, as follows:

    rails g model Test name:text
     
    rails g resource Test name:text
     
    rails g scaffold Test name:text

    What’s the difference between each of the above?

    Entering rails g model Test name:text in your command line will generate the following:

    (1) A model file test.rb in your  models directory:

    class Test < ActiveRecord::Base
    end

    (2) A migration file timestamp_create_tests.rb in your db/migrate directory:

    class CreateTests < ActiveRecord::Migration
      def change
        create_table :tests do |t|
          t.text :name
     
          t.timestamps
        end
      end
    end


    Entering rails g resource Test name:text in your command line will generate the following:

    (1) A model file test.rb in your  models directory:

    class Test < ActiveRecord::Base
    end

    (2) A migration file timestamp_create_tests.rb  in your db/migrate directory:

    class CreateTests < ActiveRecord::Migration
      def change
        create_table :tests do |t|
          t.text :name
     
          t.timestamps
        end
      end
    end

    (3) a tests_controller.rb file in your  controllers directory.  This controller will be an empty shell:

    class TestsController < ApplicationController
    end

     (4) resources :tests routes in your routes.rb file.



    Entering rails g scaffold Test name:text in your command line will generate the following:

    (1) A model file test.rb in your  models directory:

    class Test < ActiveRecord::Base
    end

    (2) A migration file timestamp_create_tests.rb in your db/migrate directory:

    class CreateTests < ActiveRecord::Migration
      def change
        create_table :tests do |t|
          t.text :name
     
          t.timestamps
        end
      end
    end

    (3) A tests_controller.rb file in your  controllers directory.  When a scaffold is generated, seven public methods and two private methods will be added to your controller:

    (4) resources :tests routes in your routes.rb file.

    (5) Seven corresponding view files in your  views directory: (a) _form.html.erb, (b) edit.html.erb, (c) index.html.erb, (d) index.json.jbuilder, (e) new.html.erb, (f) show.html.erb and (g) show.json.jbuilder. Each view will contain html and embedded ruby.

  • 相关阅读:
    Jmeter中使用HTTP信息头管理器发送json格式请求体的接口
    Jmeter工具使用初体验
    python接口测试中常见的两种接口依赖处理方式
    https类型接口,关闭认证(verify=False)情况下消除告警的方法
    当接口请求体带有中文字符时,执行脚本报错的解决办法
    java 温习之字符串(获取两个字符串的最大公共字串)
    java读取.properties配置文件的几种方法
    git
    eclipse安装git
    eclipse 安装springide插件
  • 原文地址:https://www.cnblogs.com/gdpdroid/p/6872197.html
Copyright © 2020-2023  润新知