我在模型的更新方法中遇到了双重渲染错误,我无法摆脱它。
这是控制器的代码
class Admin::CmsHappeningNowFeatureController < ApplicationController
def index
# Various model data retrieval...
render 'admin/cms/hn_features/index', layout: 'admin_cms'
end
# Works fine
def edit
@feature = CmsHappeningNowFeature.find_by_id(params[:id])
render 'admin/cms/hn_features/feature', layout: 'admin_cms'
end
# Throws a AbstractController::DoubleRenderError upon submission
def update
@feature = CmsHappeningNowFeature.find_by_id(params[:id])
@feature.attributes = params[:cms_happening_now_feature]
if @feature.save
redirect_to(:action => index, :notice => "Successfully updated feature.") and return
end
render 'admin/cms/hn_features/feature', layout: 'admin_cms'
end
# ... rest of the class
end
重定向到另一个控制器时,问题消失了。似乎重定向到同一个控制器使 rails 执行该方法而无需实际发送重定向。通过查看日志,在提交更新表单时,update
被调用,然后index
被调用,render
执行 from index然后发出重定向并失败。
我错过了什么?解决方法是什么?