rails 4:AbstractController :: DoubleRenderError,格式为

时间:2015-10-28 17:02:13

标签: ruby-on-rails-4 rendering

我正在通过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< ApplicationController的      respond_to: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评论,我更新了我的创建代码,并且我还添加了响应者gem(rails版本4.2.4:不兼容发行说明 - 3.2 respond_with / Class-Level respond_to)

  

respond_with并且相应的类级别respond_to已被移动到响应者gem。添加宝石'响应者','〜&gt; 2.0'到您的Gemfile使用它:

my:new和:现在创建代码

    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...

2 个答案:

答案 0 :(得分:0)

正如问题评论中所解释的那样,行动代码没有问题。

但是,其中一个after_filter正在调用render / redirect_to,这会导致AbstractController::DoubleRenderError错误。

就作者的问题而言,它似乎来自Pundit after_filters。

一种可能的解决方案是将这些过滤器用作before_filters而不是after_filters。实际上,当before_filter调用render或redirect_to时,操作代码不会被执行,并且不可能得到AbstractController::DoubleRenderError错误。

答案 1 :(得分:-1)

  

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 时,您应该使用 强参数

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)