【发布时间】:2012-11-09 02:15:10
【问题描述】:
我正在使用 curl 手动测试 Rails 3 的 Api 创建操作,但我不断收到 Internal server error(500)。我有一个产品模型,这是我的控制器中的创建操作和我尝试过的命令....
#command
curl -i -H "Accept: application/json" -X POST -d "product[product_name]=dvd" http://localhost:3000/api/products
#controller
module Api
class ProductsController < ApplicationController
respond_to :json
def create
@product = Product.new(params[:product])
respond_to do |format|
if @product.save
format.html { redirect_to @product, notice: 'Product was successfully created.' }
format.json { render json: @product, status: :created, location: @product }
else
format.html { render action: "new" }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
end
end
我在这里缺少什么?提前谢谢你。
【问题讨论】:
-
您可以在服务器日志中看到错误消息。 `
-
在日志中说
WARNING: Can't verify CSRF token authenticity Completed 500 Internal Server Error in 0ms ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes: product_name): app/controllers/api/products_controller.rb:72:innew' app/controllers/api/products_controller.rb:72:increate'
标签: ruby-on-rails ruby-on-rails-3 rest curl