我試圖張貼JSON消息,一個Rails 4.1.1服務器,但未能由於不允許的參數。我也使用Mongoid,並通過POST和application/json的內容類型提交。的Json後的hasMany到Rails 4失敗,未經許可參數
這裏是我的域名:
class Sale
include Mongoid::Document
include Mongoid::Timestamps
field :internalId, type: String
embeds_many :saleItems
accepts_nested_attributes_for :saleItems
end
這裏的控制器代碼:
def sale_params
params.require(:sale).permit(:internalId, :parentInternalId, :externalId, :internalIdForStore, :internalIdForCustomer, :sendReceiptType, :saleItems)
end
# POST /sales
# POST /sales.json
def create
@sale = Sale.new(sale_params)
#####################
puts "parent: "
puts @sale.inspect
puts "collections: "
@sale.saleItems.each do |si|
puts "collection here"
puts si.inspect
end
respond_to do |format|
if @sale.save
format.html { redirect_to @sale, notice: 'Sale was successfully created.' }
format.json { render action: 'show', status: :created, location: @sale }
else
format.html { render action: 'new' }
format.json { render json: @sale.errors, status: :unprocessable_entity }
end
end
end
我已經成功保存軌道集合saleItems罰款外,只是使用與收藏成功節省了Ruby腳本通過Mongoid。
這裏的JSON內容:
{
"sale" : {
"internalId":"77E26804-03CC-4CA9-9184-181C2D8CB02A"
"saleItems" : [
{
"inventoryName" : "inv 1"
},
{
"inventoryName" : "inv 2"
}
]
}
}
我試過,但沒有運氣。我已經嘗試了一系列各種許可證聲明,但必須是強參數問題。 – 2014-12-13 06:30:24
2件事:你可以發佈你的日誌?你可能想考慮轉換到Ruby的標準命名約定的領域。查看https://github.com/bbatsov/ruby-style-guide開始。這可能與你的問題有關,因爲你的參數如何匹配。 – jjk 2014-12-14 23:12:39
我想通了,所以我發佈了一個答案,但你的Ruby風格指南是值得的。我是ruby的新手,所以我將一些Java的行李帶到ruby/rails中。這個問題是由於沒有圍繞收集物品{}。看到我上面的答案。謝謝 – 2014-12-16 21:22:04