如何在Rails中将数组发布到postgres?

时间:2017-04-05 18:50:44

标签: ruby-on-rails postgresql activerecord

我无法创建将数组插入数据库。正确输入所有其他字段。我收到以下消息:Completed 406 Not Acceptable in 30ms (ActiveRecord: 21.8ms)ActionController::UnknownFormat (ActionController::UnknownFormat

这是离开前端时出现的对象:

{  dealer_name: 'Dealer Name',
   customer_first_name: 'John',
   customer_last_name: 'Doe',
   customer_phone_number: '555-555-5555',
   customer_email: 'john.doe@domain.com',
   notes: 'some notes about this transaction',
   attachmentUrls: [ 
     'https://path-to-s3/file-name1.jpg',
     'https://path-to-s3/file-name2.jpg',
     'https://path-to-s3/file-name3.jpg'
   ]
}

以下是每个日志进入Rails后端:

Parameters: {"dealer_name"=>"Dealer Name", "customer_first_name"=>"John", "customer_last_name"=>"Doe", "customer_phone_number"=>"555-555-5555", "customer_email"=>"john.doe@domain.com", "notes"=>"some notes about this transaction", "attachmentUrls"=>['https://path-to-s3/file-name1.jpg','https://path-to-s3/file-name2.jpg', 'https://path-to-s3/file-name3.jpg']}

以下是更多日志:

 SQL (1.0ms)  INSERT INTO "military_discounts" ("dealer_name", "customer_first_name", "customer_last_name", "customer_phone_number", "customer_email", "notes", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"  [["dealer_name", "Dealer Name"], ["customer_first_name", "John"], ["customer_last_name", "Doe"], ["customer_phone_number", "555-555-5555"], ["customer_email", "john.doe@domain.com"], ["notes", "notes about this transaction"], ["created_at", 2017-04-05 17:58:56 UTC], ["updated_at", 2017-04-05 17:58:56 UTC]]

这是我的API控制器:

class Api::MilitaryDiscountsController < ApplicationController
  # before_action :set_military_discount, only: [:show, :edit, :update, :destroy]

  # POST /military_discounts
  # POST /military_discounts.json
  def create
    @military_discount = MilitaryDiscount.new(military_discount_params)

    respond_to do |format|
      if @military_discount.save
        format.json { render json: @military_discount.to_json, status: :created  }
     }
      else
        format.json { render json: @military_discount.errors, status: :unprocessable_entity }
      end
    end
  end

  private   
    # Never trust parameters from the scary internet, only allow the white list through.
    def military_discount_params
      params.require(:military_discount).permit(:dealer_name, :customer_first_name, :customer_last_name, :customer_phone_number, :customer_email, :notes, :attachmentUrls)
    end
end

Rails迁移文件,DB是postgresql:

class CreateMilitaryDiscounts < ActiveRecord::Migration[5.0]
  def change
    create_table :military_discounts do |t|
      t.string :dealer_name
      t.string :customer_first_name
      t.string :customer_last_name
      t.string :customer_phone_number
      t.string :customer_email
      t.text :notes
      t.text :attachmentUrls, array: true, default: []
      t.timestamps
    end
  end
end

我错过了什么?如何将attachmentUrls数组插入数据库?

0 个答案:

没有答案