我可以从一个控制器向Rails api控制器上的ruby提交表单数据吗?

时间:2019-03-07 14:36:23

标签: ruby-on-rails

我对此还比较陌生,所以如果我不遵守所有规则或以适当的方式发布信息,请多多包涵。长话短说,我有一个Rails应用程序通过api控制器接收数据。我可以向邮递员发帖,并且一切正常。但是,我发现需要手动输入一些度量,并将数据添加到现有度量的列中。

我可以将表单数据提交到与api控制器相同的表中吗?我可以创建一个单独的手动输入控制器,但是从两个控制器查看测量值似乎更为复杂,因为我的目标是显示最新的测量值。谢谢。

编辑-我会尽力解释得更好。我已经添加了数据库架构。我有多个传感器将数据输入到api中以跟踪垃圾箱中的级别。但是,并非所有垃圾箱都装有传感器,并且传感器可能会失效,我需要输入测量值。我可以用邮递员来做,但是希望能够建立一个表格,让我可以像使用“ bins”控制器一样,将参数输入到测量表中。另外,我仅从事Rails和基于Web的编程工作了2个月。我的经验很少,但是学到了很多东西。谢谢。

ActiveRecord::Schema.define(version: 2019_03_06_131737) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "bins", force: :cascade do |t|
    t.string "name"
    t.decimal "diameter"
    t.decimal "total_height"
    t.decimal "inside_height_diff"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.decimal "response_angle"
    t.decimal "hopper_height"
    t.integer "inside_offset"
    t.integer "outside_offset"
    t.decimal "ring_height"
  end

  create_table "measurements", force: :cascade do |t|
    t.integer "outside"
    t.integer "inside"
    t.bigint "bin_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "comment"
    t.boolean "outside_fill"
    t.boolean "inside_fill"
    t.index ["bin_id"], name: "index_measurements_on_bin_id"
  end

  create_table "shipments", force: :cascade do |t|
    t.integer "ticket"
    t.date "ship_date"
    t.time "ship_time"
    t.string "commodity"
    t.string "owner"
    t.string "carrier"
    t.decimal "gross"
    t.decimal "tare"
    t.decimal "moisure"
    t.decimal "dock"
    t.decimal "test_weight"
    t.decimal "vom"
    t.string "comment"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  add_foreign_key "measurements", "bins"
end

class Api::MeasurementsController < ApplicationController
  skip_forgery_protection

  def index
    measurements = Measurement.all
    render json: measurements
  end

  def create
    measurement = Measurement.create(
      outside: params[:outside],
      inside: params[:inside],
      bin_id: params[:bin_id],
      outside_fill: params[:outside_fill],
      inside_fill: params[:inside_fill],
      comment: params[:comment]
    )
    render json: measurement
  end

  def delete
    measurement = Measurement.find(params[:id])
    measurement.destroy
  end

  def update
    measurement = Measurement.find(params[:id])
    measurement.outside = params[:outside]
    measurement.inside = params[:inside]
    measurement.bin_id = params[:bin_id]
    measurement.outside_fill = params [:outside_fill]
    measurement.inside_fill = params [:inside_fill]
    measurement.comment = params [:comment]
    measurement.save!
    render json: measurement
  end

end

0 个答案:

没有答案