【发布时间】:2017-07-19 15:34:18
【问题描述】:
我已经查看了至少 10 个关于此的问题并尝试了所有方法(例如 this question),但没有任何建议有效,例如:
例如:
format.json head :no_content and return
抛出此错误:
ArgumentError (wrong number of arguments (given 2, expected 1)):
虽然这样:
format.json head and return
抛出此错误:
ArgumentError (wrong number of arguments (given 0, expected 1..2)):
这是我目前在控制器动作中的内容:
def paid
@user = User.find_by(id: session['user_id'])
respond_to do |format|
if !@user.nil?
@user.is_paid = true
@user.payment_reference = payment_params[:reference]
@user.save
format.json { render head, status: :no_content } and return
else
format.json render json: { message: "failed to record payment" }, status: :unprocessable_entity
end
end
end
这可行,但在控制台中抛出错误:
No template found for PaymentsController#paid, rendering head :no_content
我不想添加一个空模板来解决这个问题。我想告诉 Rails 我要渲染 head :no_content!
This question 表明在 Rails 3.1 中默认生成的代码是这样的:
format.json { head :no_content }
但这在日志中显示了相同的确切错误。
【问题讨论】:
-
试试
format.json {head :no_content} and return -
谢谢,刚试了下,同样的错误。
-
试试
format.json { render head :no_content } -
这会引发 AbstractController::DoubleRenderError 但如果我添加“并返回”,那么我会在日志中得到缺少的模板。
-
@Pavan 谢谢,你为我指明了正确的方向!
标签: ruby-on-rails json controller