我正在通过Ajax请求创建一个组记录,并得到这个错误(在这两种情况下,@group有效或无效)
Started POST "/groups" for ::1 at 2015-10-28 17:47:08 +0100
Processing by GroupsController#create as JS
AbstractController::DoubleRenderError - Render and/or redirect were called multiple times in this action. Please.....
我不明白为什么,因为重定向和渲染不在同一块。。。
这是我的控制器
class GroupsController<应用程序控制器响应:html,:js
def create
@group = Group.new(company_id: current_user.company_id, name: params[:name] )
if @group.save?
respond_to do |format|
format.html { redirect_to groups_path, notice: 'Group was successfully created.'}
format.js
end
else
respond_to do |format|
format.html { redirect_to new_group_path }
format.js
end
end
end
...
该错误似乎是在尝试渲染create.js 时发生的
Rendered groups/create.js.erb (6.5ms)
Completed 500 Internal Server Error in 48784ms (Views: 46.2ms | ActiveRecord: 4.7ms)
这是一个非常标准的.js.erb文件
<% if @group.errors.blank? -%>
$('#groupModal').modal('hide');
<% else %>
$( "#modalGroupAlert" ).removeClass('alert-info');
$( "#modalGroupAlert" ).addClass('alert-danger');
$( "#modalGroupAlert span" ).replaceWith( "<span><%= nicer_display_errors(@group.errors) %></span>" );
$( "#modalGroupAlert" ).show();
<% end %>
发生了什么事?为什么?感谢的反馈
更新1======
根据Pavan的评论,我更新了我的创建代码,还添加了responsers gem(rails版本4.2.4:不兼容发布说明-3.2 responsd_with/Class Level responsd_to)
responsd_with和相应的类级别responsd_to已被移动到responsersgem。将gem"responsivers","~>2.0"添加到您的Gemfile以使用它:
my:new和:create代码现在是
def new
@group = Group.new()
authorize @group
respond_with @group
end
def create
@group = Group.new(group_params)
respond_to do |format|
if @group.save #remove ? here
format.html { redirect_to groups_path }
format.js
else
format.html { redirect_to new_group_path }
format.js
end
end
end
:new.js正确处理
Started GET "/groups/new.js?_=1446101853822" for ::1 at 2015-10-29 07:57:40 +0100
Processing by GroupsController#new as JS
...
Rendered groups/_new_modal_content.html.erb (15.8ms)
Rendered groups/new.js.erb (23.6ms)
Completed 200 OK in 84ms (Views: 65.7ms | ActiveRecord: 3.7ms)
但是:create仍然会引发相同的错误
Started POST "/groups" for ::1 at 2015-10-29 07:57:48 +0100
Processing by GroupsController#create as JS
...
Rendered groups/create.js.erb (0.4ms)
Completed 500 Internal Server Error in 71ms (Views: 31.6ms | ActiveRecord: 21.2ms)
AbstractController::DoubleRenderError - Render and/or redirect were called multiple times in this action. Please...
正如问题注释中所解释的,操作代码没有问题。
但是,其中一个after_filter正在调用render/redirect_to,这导致了AbstractController::DoubleRenderError
错误。
就作者的问题而言,它似乎来自Pundit after_filters。
一种可能的解决方案是将这些过滤器用作before_filters,而不是after_filter。事实上,当before_filter调用render或redirect_to时,不会执行操作代码,也不可能得到AbstractController::DoubleRenderError
错误。
AbstractController::DoubleRenderError-渲染和/或重定向在该操作中多次调用
您应该将create
操作更改为低于
def create
@group = Group.new(company_id: current_user.company_id, name: params[:name] )
respond_to do |format|
if @group.save #remove ? here
format.html { redirect_to groups_path, notice: 'Group was successfully created.'}
format.js
else
format.html { redirect_to new_group_path }
format.js
end
end
end
此外,当您使用Rails 4时,您应该使用strong参数。
private
def group_params
params.require(:group).permit(:name).merge(company_id: current_user.comapany_id)
end
并更改
@group = Group.new(company_id: current_user.company_id, name: params[:name] )
至
@group = Group.new(group_params)