format.json {render json:@ product.errors,status :: unprocessable_entity}

时间:2014-05-27 05:57:19

标签: ruby-on-rails

的用途是什么
 format.json { render json: @product, status: :created, location: @product }
 format.json { render json: @product.errors, status: :unprocessable_entity }         

在以下控制器产品代码中

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

1 个答案:

答案 0 :(得分:0)

如果请求了json,它会呈现一个json响应,就像html请求html一样。如果你在浏览器中加载它,它会给你html,但如果你正在做一些事情,你可能想要得到json。你应该使用""是什么用途?而不是"功能",这是不明确的。