Rails 4 Unprocessable Entity Error when make ajax post

Rails 4 Unprocessable Entity Error when making ajax post

我正在尝试构建一个 ajax 对象,但我遇到了一个有趣的错误...

这是我的嵌套表单字段

<%= f.fields_for :order_item do |ff| %>
    <div class="col-md-4">
        <%= ff.hidden_field :quote_id, value: @quote.id %>
        <%= ff.collection_select :category_id, Category.order(:name), :id, :name, include_blank: "Select Category", hide_label: true %>
    </div>
    <div class="col-md-6">
        <%= ff.grouped_collection_select :product_id, Category.order(:name), :products, :name, :id, :name, include_blank: true, hide_label: true %>
    </div>
    <div class="col-md-2">
        <%= ff.submit "Add Item", class:"btn-u btn-u-blue pull-right", id:"add_item" %>
    </div>
    <% end %>

这是我的 quote.js.coffee 我正在拨打 ajax 电话的地方

$('form').on 'click', '#add_item', (e) ->
e.preventDefault()
product = $('#quote_order_item_product_id :selected').val()
quote = $('#quote_order_item_quote_id').val()
console.log(product)
console.log(quote)
$.ajax '/order_items',
  type: 'POST'
  dataType: 'json'
  data: { order_item: { product_id: product, quote_id: quote } }
  success:(data) ->
    alert data.id
    return false
  error: (jqXHR, textStatus, errorThrown) ->
    alert textStatus
    return false

这是我的控制器(参数接受 product_idquote_id

def create
@order_item = OrderItem.new(order_item_params)
respond_to do |format|
  if @order_item.save
    format.json { render :show, status: :created, location: @order_item }
  else
    format.json { render json: @order_item.errors, status: :unprocessable_entity }
  end
 end
end

这是我要求的 order_item 模型

belongs_to :quote
belongs_to :product
  # validates :quantity, presence: true, numericality: { only_integer: true, greater_than: 0 }
  before_save :set_quantity
  before_save :set_unit_price

  def total_price
    unit_price * quantity
  end

  def set_unit_price
    product = Product.find(self.product_id)
    self.unit_price = product.price
  end

  def set_quantity
    if quantity.nil?
      self.quantity = 1
    end
  end

  private
  def product_present
    if product_id.nil?
      errors.add(:product, "is not valid or is not active.")
    end
  end

  def quote_present
    if quote_num.nil?
      errors.add(:quote, "is not a valid quote.")
    end
  end

  def finalize
    self[:unit_price] = unit_price
    self[:total_price] = quantity * self[:unit_price]
  end
end

这是我的路线

  resources :order_items, only: [:create, :update, :destroy]

我也尝试过以这种格式发布数据

data: { product_id: product, quote_id: quote }

您似乎进行了一些验证,因此无法保存空对象。

您总是试图保存一个空对象,因为您没有使用请求中的参数初始化它。

为了遵守 rails 约定,以您尝试过的格式发送参数:

order_item: { product_id: product, quote_id: quote }

然后在你的控制器中将你的代码更改为这个

def create
 @order_item = OrderItem.new(order_item_params)
 respond_to do |format|
  if @order_item.save
    format.json { render :show, status: :created, location: @order_item }
  else
    format.json { render json: @order_item.errors, status: :unprocessable_entity }
  end
 end
end

private

def order_item_params
params.require(:order_item).permit(:product_id, :quote_id)
end

你的问题出在这行 @order_item = OrderItem.new 需要参数(在 rails 中建议将它们列入白名单,因此使用其他方法)。

P.S。如果它仍然不起作用,请提供您对 OrderItem 模型的验证。