【发布时间】:2015-08-04 12:34:43
【问题描述】:
在我的 rails 控制器中,我必须在获得 @group 和 before_action 后检查该组不是 system。
但是我的控制器中有很多重复。我试图变成一个单独的方法,但我得到了经典:
Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".
这是我的代码的一部分,没有给我错误的单独方法。
def destroy
if @group.is_system?
render json: { errors: 'You can\'t delete a group system' }, status: 403
return
end
...
end
def update
if params[:group] && !params[:group].empty?
if @group.is_system?
render json: { errors: 'You can\'t edit a group system' }, status: 403
return
end
...
else
render json: { errors: 'Missing correct parameters' }, status: :unprocessable_entity
end
end
.....
【问题讨论】:
-
请加
callback('before_action')部分,不是很清楚你在做什么。 -
我确认您应该添加您使用的
before_action,因为我不明白您为什么会收到错误消息:Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return" -
my before_action only find do begin @group = current_store.groups.find(params[:group_id]) rescue ActiveRecord::RecordNotFound render_404 return end
标签: ruby-on-rails ruby-on-rails-4 controller refactoring jbuilder