【发布时间】:2014-12-13 04:54:41
【问题描述】:
我正在尝试将 json 消息发布到 Rails 4.1.1 服务器,但由于未经允许的参数而失败。我也在使用 Mongoid,并通过 POST 和 application/json 的内容类型提交。
这是我的域名:
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
我已经成功地在 rails 之外很好地保存了集合 saleItems,并且只使用了一个 ruby 脚本,集合通过 Mongoid 成功保存。
这是 JSON 内容:
{
"sale" : {
"internalId":"77E26804-03CC-4CA9-9184-181C2D8CB02A"
"saleItems" : [
{
"inventoryName" : "inv 1"
},
{
"inventoryName" : "inv 2"
}
]
}
}
【问题讨论】:
标签: ruby-on-rails ruby json mongoid