Has_many通过postman发送的相关json数据没有填充到rails 4中的数据库中

时间:2016-07-29 09:26:35

标签: ruby-on-rails ruby json postman

我正在尝试通过Postman发送一个Post请求,它发送我的供应商模型属性和值以及vendor_products模型属性。我的json数据看起来像这样

  {
   "name": "Raghu",
   "email": "raghu@gmail.com",
   "phone": "9008849042",
     :
     :
   "vendor_products_attributes":
      {
        [{"product_id" : 7, "copies" : 8},
         {"product_id" : 8, "copies" : 10}]
      }
    }

现在我的所有供应商模型字段都已填充,但却没有填充vendor_products模型。我使用嵌套表单和供应商和vendor_products的嵌套属性

我的协会如下

供应商 - >

    has_many :vendor_products
    has_many :products, through: :vendor_products

产品 - >

    has_many :vendor_products
    has_many :vendors, through: :vendor_products

vendor_product

    belongs_to :vendor
    belongs_to :product

Vendor.rb

 class Vendor < ActiveRecord::Base
  has_many :vendor_products
  has_many :products, through: :vendor_products
  accepts_nested_attributes_for :vendor_products, :products, 
  :allow_destroy => true
 end

我的供应商/ _form.html.erb

<%= form_for(@vendor) do |f| %>
<% if @vendor.errors.any? %>
  <div id="error_explanation">
  <h2><%= pluralize(@vendor.errors.count, "error") %> prohibited this 
vendor from being saved:</h2>

  <ul>
    <% @vendor.errors.full_messages.each do |message| %>
  <li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
:
:
:
<%= f.fields_for :vendor_products do |vproducts| %>
<div class="field">
<%= vproducts.label :product %><br>
<%= vproducts.select :product_id, 
options_from_collection_for_select(Product.all, "id", "product_name"),    
prompt: "Select something" %>
<div class="field">
<%= vproducts.label :copies %><br>
<%= vproducts.number_field :copies %>
</div>
<% end %>

<div class="actions">
<%= f.submit %>
 </div>
<% end %>

我的vendors_controller.rb

class VendorsController < ApplicationController
  before_action :set_vendor, only: [:show, :edit, :update, :destroy]
  respond_to :json

  def index
    @vendors = Vendor.all.limit(20)
  end


  def show 
  end 


  def new 
    @vendor = Vendor.new
    @product = @vendor.products.build
    @vendor.vendor_products.build
  end


  def edit
  end


  def create
    @vendor = Vendor.new(vendor_params)      

    respond_to do |format|
     if @vendor.save
        format.html { redirect_to @vendor, 
        notice: 'Vendor was successfully created.' }
       format.json { render :show, status: :created, location: @vendor    
        }
     else
       format.html { render :new }
       format.json { render json: @vendor.errors, status: 
        :unprocessable_entity }
     end
   end
  end


 def update
    respond_to do |format|
    if @vendor.update(vendor_params)
       format.html { redirect_to @vendor, notice: 'Vendor was
    successfully updated.' }
       format.json { render :show, status: :ok, location: @vendor }
    else
      format.html { render :edit }
      format.json { render json: @vendor.errors, status:
                  :unprocessable_entity }
    end
  end
 end



  private

  def set_vendor
    @vendor = Vendor.find(params[:id])
  end

  def vendor_params
     params.require(:vendor).permit(:name, :email, :phone_no, 
  :addressline1, 
  :addressline2, :landmark,
  :city, :state, :country, :pincode, :latitude, :longitude, :status, 
    product_attributes: [:product_id, :product_name, :price ], 
    vendor_products_attributes: [:vendor_product_id, :vendor_id, 
    :product_id, 
    :copies])
  end
 end

的routes.rb

  Rails.application.routes.draw do

  match '/vendors/create_vendor' => 'vendors#create' , via: [:post]

    resources :vendor_products
    resources :products
    resources :vendors      
  end

我的服务器记录

Started POST "/vendors/create_vendor" for 127.0.0.1 at 2016-07-29    
15:03:43 +0530
Processing by VendorsController#create as */*
Parameters: {"name"=>"Ragtyh", "email"=>"mahesh8mesta@gmail.com", 
"phone_no"=>"9008849042", "addressline1"=>"#7, Varsha Pg", 
"addressline2"=>"23rd main marenhalli, JP nagar 2nd phase", 
"landmark"=>"opp mayura sweets", "city"=>"Bangalore", 
"state"=>"Karnataka", "country"=>"India", "pincode"=>"560078", 
"latitude"=>12.898773, "longitude"=>77.5764094, "status"=>true, 
"vendor_products_attributes"=>[{"product_id"=>9, "copies"=>2}, 
{"product_id"=>7, "copies"=>3}], "vendor"=>{"name"=>"Ragtyh", 
"email"=>"mahesh8mesta@gmail.com", "phone_no"=>"9008849042", 
"addressline1"=>"#7, Varsha Pg", "addressline2"=>"23rd main marenhalli, 
JP nagar 2nd phase", "landmark"=>"opp mayura sweets", 
"city"=>"Bangalore", "state"=>"Karnataka", "country"=>"India", 
"latitude"=>12.898773, "longitude"=>77.5764094, "status"=>true, 
"pincode"=>"560078"}}
 [1m[35m (0.2ms)[0m  BEGIN
 [1m[36mSQL (0.5ms)[0m  [1mINSERT INTO "vendors" ("name", "email", 
"phone_no", "addressline1", "addressline2", "landmark", "city", "state", 
"country", "pincode", "latitude", "longitude", "status", "created_at", 
"updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, 
$13, $14, $15) RETURNING "id"[0m  [["name", "Ragtyh"], ["email", 
"mahesh8mesta@gmail.com"], ["phone_no", "9008849042"], ["addressline1", 
"#7, Varsha Pg"], ["addressline2", "23rd main marenhalli, JP nagar 2nd 
phase"], ["landmark", "opp mayura sweets"], ["city", "Bangalore"], 
["state", "Karnataka"], ["country", "India"], ["pincode", "560078"], 
["latitude", 12.898773], ["longitude", 77.5764094], ["status", "t"], 
["created_at", "2016-07-29 09:33:48.844763"], ["updated_at", "2016-07-29 
09:33:48.844763"]]
 [1m[35m (0.7ms)[0m  COMMIT
 Redirected to http://localhost:3000/vendors/100025 
 Completed 302 Found in 5592ms (ActiveRecord: 1.4ms)

虽然邮递员中的原始json数据填充了供应商表,但却没有填充或创建vendor_products。我怎么能解决这个问题。请注意我是铁杆新手

1 个答案:

答案 0 :(得分:1)

通过您的代码并且您的所有代码都是正确的,但问题似乎出现在您通过邮递员向控制器发出的请求中。

在请求json文件中,您没有供应商密钥,因此您可以看到日志,vendor_productsnot inside the vendor hash

"vendor_products_attributes"=>[{"product_id"=>9, "copies"=>2}, {"product_id"=>7, "copies"=>3}]

这应该在vendor哈希内部,

所以你的JSON看起来应该是这样的,

{
"vendor": {
   "name": "Raghu",
   "email": "raghu@gmail.com",
   "phone": "9008849042",
     :
     :
   "vendor_products_attributes":
      {
        [{"product_id" : 7, "copies" : 8},
         {"product_id" : 8, "copies" : 10}]
      }
    }
  } 

要实现这一目标,你应该采取一些措施,

  vendor[:name] = ""
  vendor[:name] = ""
  vendor[:phone] = ""
  ...

  ...
  vendor[:vendor_products_attributes][product_id][] = 7
  vendor[:vendor_products_attributes][copies][] = 10
  vendor[:vendor_products_attributes][product_id][] = 8
  vendor[:vendor_products_attributes][copies][] = 15

来自你的postnam。