2015-10-28 133 views
0

我創建通過Ajax請求一組記錄,並且收到此錯誤(在這兩種情況下,@group有效或無效)軌道4:一個AbstractController :: DoubleRenderError與格式

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

我不明白爲什麼作爲重定向和渲染不在相同的塊...

這裏是我的控制器

類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個=======

按帕意見,我更新了我的創建代碼,我也加入了反應寶石(導軌版本4.2.4:不相容性釋放筆記 - 3.2 respond_with/Class-Level respond_to)

respond_with和相應的級別級別respond_to已被移動到響應者gem。添加寶石 '反應', '〜> 2.0' 到你的Gemfile中使用它:

我:新:創建代碼現在

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) 

但:創建仍然提高相同的錯誤

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

你有一個before_filter或過濾後可能會調用渲染或重定向? –

+0

感謝西蒙,我有過濾器之前和之後,但他們不影響渲染...只驗證,可變設置和權限 before_filter:authenticate_user! before_action:set_group,除外:[:index,:new,:create] after_filter:verify_authorized,:except =>:index,除非:: devise_controller? after_filter:verify_policy_scoped,:only =>:index – erwin

+0

SORRY ...我錯了,你是對的! 過濾器後的專家是問題!評論他們使我的代碼運行!請添加它作爲答案,所以我可以投票支持,也許可以幫助代表運行Pundit&Rails 4.2.4 – erwin

回答

0

一個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 

另外,作爲您使用軌道4,5,您應該使用強參數

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) 
+0

感謝Pavan ...我根據您的意見更新了代碼,但我仍然收到相同的錯誤.. 當我運行Rails 4.2.4時,我還添加了響應者gem,以更好地處理respond_with,respond_to,但它不會改變行爲.... rendering create.js.erb正在引發錯誤... – erwin

+0

@erwin嘗試從控制器中刪除'respond_to:html,:js'。 – Pavan

+0

發現問題:請參閱我對Simon評論的回答... Pundit after_filter是有罪的...評論它使我的代碼運行...所以它沒有與響應者處理.... – erwin

1

如問題的意見解釋,不存在與動作的代碼沒有問題。

但是,其中一個after_filter正在調用render/redirect_to,導致出現AbstractController::DoubleRenderError錯誤。

對於作者的問題,它似乎來自Pundit的after_filters。

一個可能的解決方案是使用這些過濾器作爲before_filters而不是after_filters。實際上,當before_filter調用render或redirect_to時,操作代碼不會執行,並且不可能得到AbstractController::DoubleRenderError錯誤。

+0

完成你的答案......它仍然可以使用權威人士after_filter與JS迴應: after_filter: format.js {} skip_authorization我 – erwin

+0

有:通過跳過它在format.js響應塊verify_authorized 類似的情況下,行動創建一個父母然後更新子記錄。授權是罪魁禍首。 – Jerome