ruby 构建API接口流程代码

创建控制器

--no-assets 我们不需要生成资源文件

haima@haima-PC:/media/haima/34E401CC64DD0E28/site/go/src/ruby/rails_demo$ rails g controller api/v1/order --no-assets
      create  app/controllers/api/v1/order_controller.rb
      invoke  erb
      create    app/views/api/v1/order
      invoke  test_unit
      create    test/controllers/api/v1/order_controller_test.rb
      invoke  helper
      create    app/helpers/api/v1/order_helper.rb
      invoke    test_unit

编写控制器

app/controllers/api/v1/order_controller.rb

class Api::V1::OrderController < ApplicationController
  def show
    puts "show"
    @order = Order.find(params[:id])
    puts @order
    res = {
      meg: "ok",
      order: @order
    }
    render(json: res)
  end
end


创建model

haima@haima-PC:/media/haima/34E401CC64DD0E28/site/go/src/ruby/rails_demo$ rails g model order
      invoke  active_record
      create    db/migrate/20210715232943_create_orders.rb
      create    app/models/order.rb
      invoke    test_unit
      create      test/models/order_test.rb
      create      test/fixtures/orders.yml

编写迁移文件

db/migrate/20210715232943_create_orders.rb

class CreateOrders < ActiveRecord::Migration[5.0]
  def change
    create_table :orders, comment: '订单表'  do |t|
      t.string :sn, comment: '编号'
      t.integer :status,default:0,comment:'状态 0:否 1:是'
      t.timestamps
    end
    add_index :orders, :sn, :unique => true #唯一索引
  end
end

迁移数据表

haima@haima-PC:/media/haima/34E401CC64DD0E28/site/go/src/ruby/rails_demo$ rake db:migrate
== 20210715232943 CreateOrders: migrating =====================================
-- create_table(:orders, {:comment=>"订单表"})
   -> 0.0707s
-- add_index(:orders, :sn, {:unique=>true})
   -> 0.0704s
== 20210715232943 CreateOrders: migrated (0.1414s) ============================

创建出来的表

CREATE TABLE `orders` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `sn` varchar(255) DEFAULT NULL COMMENT '编号',
  `status` int(11) DEFAULT '0' COMMENT '状态 0:否 1:是',
  `created_at` datetime NOT NULL,
  `updated_at` datetime NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `index_orders_on_sn` (`sn`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单表';

创建随机数据

安装组件

文档地址:
https://github.com/faker-ruby/faker

编写Gemfile
添加
gem 'faker'

安装gem:
bundle install

现在调整seeds.rb :
db / seeds.rb

10.times do
  # order = Order.create({sn: Faker::Book.title,status:Faker::Boolean.boolean})
  order = Order.create({sn: Faker::Code.unique.npi,status:Faker::Boolean.boolean})
end

最后,加载您的数据:

rails db:seed

效果:

查看路由

rake -T
rake routes

api_v1_order_index GET    /api/v1/order(.:format)     api/v1/order#index
                   POST   /api/v1/order(.:format)     api/v1/order#create
      api_v1_order GET    /api/v1/order/:id(.:format) api/v1/order#show
                   PATCH  /api/v1/order/:id(.:format) api/v1/order#update
                   PUT    /api/v1/order/:id(.:format) api/v1/order#update
                   DELETE /api/v1/order/:id(.:format) api/v1/order#destroy

访问:
localhost:3000/api/v1/order/1

{
    "meg":"ok",
    "order":{
        "id":1,
        "sn":"The Curious Incident of the Dog in the Night-Time",
        "status":0,
        "created_at":"2021-07-15T23:56:22.000Z",
        "updated_at":"2021-07-15T23:56:22.000Z"
    }
}

https://blog.csdn.net/cunjie3951/article/details/106920302
https://www.cnblogs.com/lv-books/p/6479158.html

[Haima的博客] http://www.cnblogs.com/haima/
posted @ 2021-06-07 00:38  HaimaBlog  阅读(31)  评论(0编辑  收藏  举报