控制器中部分和布局的 Rails 渲染 [英] Rails render of partial and layout in controller

查看:27
本文介绍了控制器中部分和布局的 Rails 渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

我试过了

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

但是我得到一个错误

Template is missing
Missing partial layouts/_application 

为什么在我指定布局时要寻找部分 _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

谢谢

编辑

阅读它所说的文档

请注意,分音的布局遵循与常规分音相同的前导下划线命名,并与它们所属的分音放在同一文件夹中(不在主布局文件夹中)."

"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)."

但我想应用默认布局

推荐答案

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

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.

这可能是您的代码不起作用的原因,您可以使用渲染模板.

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.

希望这有效!

这篇关于控制器中部分和布局的 Rails 渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆