[英]Rails - Intercept respond_with
I am using this 3rd party controller: 我正在使用此第三方控制器:
class LibController
def update
# 29 lines of code
respond_with resource
end
end
I want to do something other than the respond_with
at the end. 最后,我想做其他事情而不是
respond_with
。 But I don't want to just copy/paste all 29 lines into MyController.update
. 但是我不想只复制/粘贴所有29行到
MyController.update
。 Unfortunately I can't figure out a way to render or redirect anywhere else: 不幸的是,我无法找到一种方法来渲染或重定向其他地方:
class MyController < LibController
def update
super
redirect_to somewhere_else
end
end
I get a DoubleRenderError: Render and/or redirect were called multiple times in this action
. 我收到
DoubleRenderError: Render and/or redirect were called multiple times in this action
。 I assume this is because respond_with
calls render
immediately. 我认为这是因为
respond_with
调用会立即render
。 Is there a way to block/prevent that? 有没有办法阻止/防止这种情况?
Thanks! 谢谢!
I think you are doing a twice redirection. 我认为您正在执行两次重定向。 Try to remove one redirection on your update method.
尝试删除您的更新方法上的一种重定向。
Check sample code below that shows equivalent response when using respond_with . 检查样品代码下面示出等效响应使用respond_with时。
def create
@user = User.new(params[:user])
flash[:notice] = 'User was successfully created.' if @user.save
respond_with(@user)
end
Which is exactly the same as: 完全相同:
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
flash[:notice] = 'User was successfully created.'
format.html { redirect_to(@user) }
format.xml { render xml: @user, status: :created, location: @user }
else
format.html { render action: "new" }
format.xml { render xml: @user.errors, status: :unprocessable_entity }
end
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.