渲染动作:在控制器的更新方法中编辑不重新渲染表单

时间:2014-05-23 13:22:44

标签: ruby-on-rails forms controller render

当用户使用编辑表单成功更新记录时,我希望它通过通知重新呈现表单(就像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

我应该用什么代替渲染动作:“编辑”?

1 个答案:

答案 0 :(得分: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)