【问题标题】:Render action: edit not re-rendering form in update method of controller渲染动作:在控制器的更新方法中编辑不重新渲染表单
【发布时间】:2025-12-28 06:50:11
【问题描述】:
当用户使用编辑表单成功更新记录时,我希望它重新呈现带有通知的表单(就像 wordpress 仪表板一样)。现在,当有人成功更新页面记录时,视图不会更新。这是我的 Admin::Page 控制器中的更新方法:
def update
@page = Page.find_by_permalink!(params[:id])
if @page.published? && @page.published_at == nil
@page.published_at = Time.now
elsif !@page.published? && @page.published_at != nil
@page.published_at = nil
end
if @page.update_attributes(page_params)
render action: "edit", notice: 'Page was successfully updated.'
else
render action: "edit"
end
end
我应该用什么来代替渲染动作:“编辑”?
【问题讨论】:
标签:
ruby-on-rails
forms
controller
render
【解决方案1】:
试试这个:-
def update
@page = Page.find_by_permalink!(params[:id])
if @page.published? && @page.published_at == nil
@page.published_at = Time.now
elsif !@page.published? && @page.published_at != nil
@page.published_at = nil
end
if @page.update_attributes(page_params)
redirect_to({ action: 'edit',id: @page }, notice: 'Page was successfully updated.') #works on rails >=3.1 OR try the commented line below.
#redirect_to edit_page_path(@page), notice: 'Page was successfully updated.'
else
render action: "edit"
end
end
将 edit_page_path(@page) 更改为 编辑操作的路径。