ruby 控制器中部分和布局的 Rails 渲染
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25738836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails render of partial and layout in controller
提问by Richlewis
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)."
“请注意,分音的布局遵循与常规分音相同的前导下划线命名,并与它们所属的分音放在同一文件夹中(不在主布局文件夹中)。”
But i want the default layout to be applied
但我希望应用默认布局
采纳答案by Deepak Kumar Jha
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 调用的控制器操作)使用相同的部分模板。默认情况下,不使用当前布局。
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 !
希望这有效!
回答by Richlewis
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
最后(我知道这是一个 hack)但我在我的布局中创建了一个名为 _partial_layout_wrapper 的部分,它是布局/应用程序文件的精确副本,并在我的控制器中使用它
render partial: 'shared/company_signup_form', layout: 'partial_layout_wrapper'
this works but surely this cannot be the way ?
这行得通,但肯定不是这样吗?
回答by Plamena Gancheva
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).
Also note that explicitly specifying :partial is required when passing additional options such as :layout.
部分布局
局部可以使用自己的布局文件,就像视图可以使用布局一样。例如,您可以像这样调用部分:
<%= render partial: "link_area", layout: "graybar" %>这将查找名为 _link_area.html.erb 的部分并使用布局 _graybar.html.erb 呈现它。请注意,分音的布局遵循与常规分音相同的前导下划线命名,并与它们所属的分音放在同一文件夹中(不在主布局文件夹中)。
另请注意,在传递诸如 :layout 之类的其他选项时,需要明确指定 :partial。
So render partial: 'shared/company_signup_form', layout: 'layouts/application'is looking for layouts/_application.html.erb
所以render partial: 'shared/company_signup_form', layout: 'layouts/application'正在寻找 layouts/_application.html.erb

