我的关联的Rails强对手没有正常工作

时间:2016-06-23 19:53:09

标签: ruby-on-rails ruby-on-rails-4 strong-parameters

我在建立自己的协会时遇到了困难。出于某种原因,我的pickup_addressdropoff_address属性不属于order_params

顺序

class Order < ActiveRecord::Base
  has_one :delivery
  has_one :courier, through: :delivery
  has_one :pickup_address, foreign_key: 'pickup_address_id', class_name: 'Order', autosave: true
  has_one :dropoff_address, foreign_key: 'dropoff_address_id', class_name: 'Order'

  accepts_nested_attributes_for :pickup_address, :dropoff_address

  validates_presence_of :order_number, :deliver_at, :pickup_name,
                        :pickup_phone_number, :pickup_address_id,
                        :dropoff_name, :dropoff_phone_number,
                        :dropoff_address_id
end

订单控制器:

def create
  order = Order.new(order_params)
  binding.pry
  order.build_pickup_address(params[:order][:pickup_address])
  order.build_dropoff_address(order_params)

  if order.save
    render json: order, status: :created
  else
    render json: { errors: order.errors }, status: 422
  end

end

private

def order_params
  params.require(:order).permit(:order_number, :deliver_at, :pickup_name, :pickup_phone_number, :dropoff_phone_number, :dropoff_name, :pickup_name,
                                pickup_address: [:line1, :line2, :state, :city, :zip, :latitude, :longitude])
end

  {"order"=>
    {"order_number"=>"9x6mu0t8xr",
     "deliver_at"=>"2016-06-24",
     "pickup_name"=>"Torey Boehm",
     "pickup_phone_number"=>"450-625-1582 x0561",
     "dropoff_phone_number"=>"216-893-1930 x4414",
     "dropoff_name"=>"Stuart Hauck",
     "pickup_address"=>
      {"line1"=>"81241 Haleigh Course",
       "city"=>"Adahview",
       "state"=>"MD",
       "zip"=>"33048-6371",
       "latitude"=>"38.9676548",
       "longitude"=>"-77.0766498"},
     "dropoff_address"=>
      {"line1"=>"7762 Goyette Garden",
       "city"=>"Ninabury",
       "state"=>"MD",
       "zip"=>"43383-7331",
       "latitude"=>"38.9676548",
       "longitude"=>"-77.0766498"}},
   "format"=>"json",
   "controller"=>"api/v1/orders",
   "action"=>"create"}

order_params

order_params
=> {"order_number"=>"bkrojbmduu",
 "deliver_at"=>"2016-06-25",
 "pickup_name"=>"Arne Hills",
 "pickup_phone_number"=>"(361) 350-3872 x7703",
 "dropoff_phone_number"=>"213.583.0194",
 "dropoff_name"=>"Mr. Kyleigh West"}

正如您所看到的,我的order_params没有返回我的嵌套属性。

0 个答案:

没有答案