我創建通過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...
你有一個before_filter或過濾後可能會調用渲染或重定向? –
感謝西蒙,我有過濾器之前和之後,但他們不影響渲染...只驗證,可變設置和權限 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
SORRY ...我錯了,你是對的! 過濾器後的專家是問題!評論他們使我的代碼運行!請添加它作爲答案,所以我可以投票支持,也許可以幫助代表運行Pundit&Rails 4.2.4 – erwin