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

geektony · March 13, 2015 · Last by limkurn replied at March 13, 2015 · 969 hits
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",
        }
    ]
}
You need to Sign in before reply, if you don't have an account, please Sign up first.