如何在提交表单时指定要呈现的布局并且验证失败?

时间:2022-01-13 07:45:44

In my app, new and edit actions of a controller should render different templates rather than the default layout like the following (it is not a normal rails app but an engine though).

在我的应用程序中,控制器的新动作和编辑动作应该呈现不同的模板,而不是像下面的默认布局(它不是普通的rails应用程序,而是引擎)。

module Blorgh
  class ArticlesController < ApplicationController

    layout Blorgh.config.layout # Configured in initializer

    ...

    # GET /articles/new
    def new
      @article = Article.new
      render :new, layout: "blorgh/new"
    end

    # GET /articles/1/edit
    def edit
      render :edit, layout: "blorgh/edit"
    end

    ...
  end
end

It works as expected, but when validation failed on POST create or PATCH update action via submitting a form in new or edit page, they are rendered with the default layout.

它按预期工作,但是当通过在新页面或编辑页面中提交表单在POST创建或PATCH更新操作上验证失败时,它们将使用默认布局呈现。

How to specify the layout in this case?

在这种情况下如何指定布局?

  • Ruby 2.2.0
  • Ruby 2.2.0
  • Rails 4.2.0
  • Rails 4.2.0

Thanks in advance.

提前致谢。

1 个解决方案

#1


3  

The way to do it is the same as in new and edit actions:

这样做的方法与新操作和编辑操作相同:

def update
  # ...
  render :edit, layout: 'blorgh/edit'
end

def create
  # ...
  render :new, layout: 'blorgh/new'
end

BTW, in your new and edit actions, you don't have to indicate template to render manually. It's ok if you have:

顺便说一句,在新的和编辑操作中,您不必指定要手动渲染的模板。没关系,如果你有:

def new
  # ...
  render layout: 'blorgh/new'
end

def edit
  # ...
  render layout: 'blorgh/edit'
end

#1


3  

The way to do it is the same as in new and edit actions:

这样做的方法与新操作和编辑操作相同:

def update
  # ...
  render :edit, layout: 'blorgh/edit'
end

def create
  # ...
  render :new, layout: 'blorgh/new'
end

BTW, in your new and edit actions, you don't have to indicate template to render manually. It's ok if you have:

顺便说一句,在新的和编辑操作中,您不必指定要手动渲染的模板。没关系,如果你有:

def new
  # ...
  render layout: 'blorgh/new'
end

def edit
  # ...
  render layout: 'blorgh/edit'
end