Rails渲染部分和布局在控制器中

时间:2022-05-21 20:19:03

I am overriding the create action of the devise Registrations Controller. I have two forms for signup, individual or company, a company has a field called company_form set to true that differentiates the two forms.

我正在重写设计注册控制器的创建操作。我有两种注册形式,个人或公司,公司有一个名为company_form的字段设置为true,以区分这两种形式。

Upon form validation I would like the correct form to render (previously it was going back to the default form no matter what form i was using).

在表单验证时,我希望呈现正确的表单(以前无论我使用什么形式,它都会回到默认表单)。

I am having an issue where just the partial is being rendered (obvious as i am only rendering the partial), but I need the layouts/application file to be rendered aswell.

我有一个问题,只有部分被渲染(显而易见,因为我只渲染部分),但我需要渲染布局/应用程序文件。

class RegistrationsController < Devise::RegistrationsController
  def create
  <!-- Other devise code here -->
    if resource.company_form
      render partial: 'shared/company_signup_form'
    else
      render partial: '/shared/individual_signup_form'
    end
  end
end

I have tried

我努力了

if resource.company_form
    render partial: 'shared/company_signup_form', layout: 'layouts/application'
  else
    render partial: '/shared/individual_signup_form', layout: 'layouts/application
  end

But i get an error

但我得到一个错误

Template is missing
Missing partial layouts/_application 

Why is it looking for a partial _application when I specified layout and how can i get the correct layout to be applied please

当我指定布局时为什么要查找部分_application,如何才能获得正确的布局

Thanks

Edit

Reading through the documentation it says

阅读它说的文档

"Note that layouts for partials follow the same leading-underscore naming as regular partials, and are placed in the same folder with the partial that they belong to (not in the master layouts folder)."

“请注意,partials的布局遵循与常规partials相同的前导下划线命名,并且与它们所属的部分放在同一个文件夹中(不在master layouts文件夹中)。”

But i want the default layout to be applied

但我希望应用默认布局

3 个解决方案

#1


5  

Partial rendering in a controller is most commonly used together with Ajax calls that only update one or a few elements on a page without reloading. Rendering of partials from the controller makes it possible to use the same partial template in both the full-page rendering (by calling it from within the template) and when sub-page updates happen (from the controller action responding to Ajax calls). By default, the current layout is not used.

控制器中的部分呈现最常与Ajax调用一起使用,Ajax调用仅更新页面上的一个或几个元素而无需重新加载。从控制器渲染部分可以在整页渲染中使用相同的部分模板(通过在模板中调用它)和子页面更新发生时(从响应Ajax调用的控制器操作)。默认情况下,不使用当前布局。

This may be the reason your code is not working you can use rendering template.

这可能是您的代码无法工作的原因,您可以使用渲染模板。

Template rendering works just like action rendering except that it takes a path relative to the template root. The current layout is automatically applied.

模板渲染的工作方式与动作渲染类似,只是它采用相对于模板根的路径。将自动应用当前布局。

if resource.company_form
   render :template => "shared/company_signup_form"
else
   render :template => "shared/individual_signup_form"   
end

** REMOVE UNDERSCORE from your partail name because you are using this as a template.

**从您的部分名称中删除UNDERSCORE,因为您将此作为模板使用。

Hope this works !

希望这个有效!

#2


2  

In the end (and i know this is a hack) but i created a partial called _partial_layout_wrapper in my layouts that was an exact copy of the layouts/application file and used this in my controller

最后(我知道这是一个黑客),但我在我的布局中创建了一个部分名为_partial_layout_wrapper的布局/应用程序文件的精确副本,并在我的控制器中使用它

render partial: 'shared/company_signup_form', layout: 'partial_layout_wrapper'

this works but surely this cannot be the way ?

这可行,但肯定不是这样的?

#3


1  

Could you post your whole controller? Rails renders by default layout/application.html.erb if another layout is not specified.

你能发布整个控制器吗?如果未指定其他布局,则Rails默认呈现layout / application.html.erb。

From the ror guides:

来自ror指南:

Partial Layouts

A partial can use its own layout file, just as a view can use a layout. For example, you might call a partial like this:

部分可以使用自己的布局文件,就像视图可以使用布局一样。例如,您可以像这样调用部分:

<%= render partial: "link_area", layout: "graybar" %>

This would look for a partial named _link_area.html.erb and render it using the layout _graybar.html.erb. Note that layouts for partials follow the same leading-underscore naming as regular partials, and are placed in the same folder with the partial that they belong to (not in the master layouts folder).

这将查找名为_link_area.html.erb的部分名称,并使用布局_graybar.html.erb进行渲染。请注意,partials的布局遵循与常规partials相同的前导下划线命名,并且与它们所属的部分放在同一文件夹中(不在master layouts文件夹中)。

Also note that explicitly specifying :partial is required when passing additional options such as :layout.

另请注意,显式指定:传递其他选项时需要部分,例如:layout。

So render partial: 'shared/company_signup_form', layout: 'layouts/application'is looking for layouts/_application.html.erb

所以渲染部分:'shared / company_signup_form',布局:'layouts / application'正在寻找布局/ _application.html.erb

#1


5  

Partial rendering in a controller is most commonly used together with Ajax calls that only update one or a few elements on a page without reloading. Rendering of partials from the controller makes it possible to use the same partial template in both the full-page rendering (by calling it from within the template) and when sub-page updates happen (from the controller action responding to Ajax calls). By default, the current layout is not used.

控制器中的部分呈现最常与Ajax调用一起使用,Ajax调用仅更新页面上的一个或几个元素而无需重新加载。从控制器渲染部分可以在整页渲染中使用相同的部分模板(通过在模板中调用它)和子页面更新发生时(从响应Ajax调用的控制器操作)。默认情况下,不使用当前布局。

This may be the reason your code is not working you can use rendering template.

这可能是您的代码无法工作的原因,您可以使用渲染模板。

Template rendering works just like action rendering except that it takes a path relative to the template root. The current layout is automatically applied.

模板渲染的工作方式与动作渲染类似,只是它采用相对于模板根的路径。将自动应用当前布局。

if resource.company_form
   render :template => "shared/company_signup_form"
else
   render :template => "shared/individual_signup_form"   
end

** REMOVE UNDERSCORE from your partail name because you are using this as a template.

**从您的部分名称中删除UNDERSCORE,因为您将此作为模板使用。

Hope this works !

希望这个有效!

#2


2  

In the end (and i know this is a hack) but i created a partial called _partial_layout_wrapper in my layouts that was an exact copy of the layouts/application file and used this in my controller

最后(我知道这是一个黑客),但我在我的布局中创建了一个部分名为_partial_layout_wrapper的布局/应用程序文件的精确副本,并在我的控制器中使用它

render partial: 'shared/company_signup_form', layout: 'partial_layout_wrapper'

this works but surely this cannot be the way ?

这可行,但肯定不是这样的?

#3


1  

Could you post your whole controller? Rails renders by default layout/application.html.erb if another layout is not specified.

你能发布整个控制器吗?如果未指定其他布局,则Rails默认呈现layout / application.html.erb。

From the ror guides:

来自ror指南:

Partial Layouts

A partial can use its own layout file, just as a view can use a layout. For example, you might call a partial like this:

部分可以使用自己的布局文件,就像视图可以使用布局一样。例如,您可以像这样调用部分:

<%= render partial: "link_area", layout: "graybar" %>

This would look for a partial named _link_area.html.erb and render it using the layout _graybar.html.erb. Note that layouts for partials follow the same leading-underscore naming as regular partials, and are placed in the same folder with the partial that they belong to (not in the master layouts folder).

这将查找名为_link_area.html.erb的部分名称,并使用布局_graybar.html.erb进行渲染。请注意,partials的布局遵循与常规partials相同的前导下划线命名,并且与它们所属的部分放在同一文件夹中(不在master layouts文件夹中)。

Also note that explicitly specifying :partial is required when passing additional options such as :layout.

另请注意,显式指定:传递其他选项时需要部分,例如:layout。

So render partial: 'shared/company_signup_form', layout: 'layouts/application'is looking for layouts/_application.html.erb

所以渲染部分:'shared / company_signup_form',布局:'layouts / application'正在寻找布局/ _application.html.erb