Rails ForbiddenAttributesError && 嵌套 && 創建對象

geektony · 2015年03月13日 · 最后由 limkurn 回复于 2015年03月13日 · 968 次阅读
def create
    @order = current_client.orders.new(order_params)
    begin
        @order.save!
        products = params[:products]
        products.each do |product_hash|
            @order.products.create!(product_hash)
        end
    rescue
        return head :bad_request
    end
end

create 的時候發生 ForbiddenAttributesError

products 是嵌套在 order 的 JSON 裡面的數組

{
    "xxx" : "xxxxxx",
    "products": [
        {
            "xxx": "xxxx",
        }
    ]
}
需要 登录 后方可回复, 如果你还没有账号请 注册新账号