gpt4 book ai didi

html - Rails 使用 content_for 并渲染相同的布局两次

转载 作者:行者123 更新时间:2023-11-28 00:31:23 25 4
gpt4 key购买 nike

我正在使用一个具有非常特定布局的主题,我想为您的 Rails 表单提供一种故障保护方式。

我有一个布局 app/views/shared/forms/fields/_layout.html.erb

<div class="js-form-message mb-4">
<div class="js-focus-state input-group u-form">
<div class="input-group g-brd-primary--focus">
<%= yield(:field) %>
</div>
</div>
</div>

我有两个偏音。第一部分:app/views/shared/forms/fields/_email.html.erb

<% form = locals[:form] %>
<% locals[:required] = locals[:required].nil? ? true : locals[:required] %>
<% locals[:placeholder] = locals[:placeholder] || t('forms.shared.email.placeholder') %>


<%= render layout: "shared/forms/fields/layout", locals: locals do %>
<% content_for(:field) do %>
<%= form.email_field :email,
placeholder: locals[:placeholder],
class: "form-control g-py-15 g-px-15",
"data-error-class"=>"u-has-error-v1-3",
"data-success-class"=>"u-has-success-v1-2",
"data-msg-email" => t('forms.shared.email.validate'),
"data-msg" => t('forms.shared.required'),
autofocus: locals[:autofocus],
required: locals[:required] %>
<% end %>
<% end %>

第二部分:app/views/shared/forms/fields/_login.html.erb

<% form = locals[:form] %>
<% locals[:required] = locals[:required].nil? ? true : locals[:required] %>
<% locals[:placeholder] = locals[:placeholder] || t('forms.shared.login.placeholder') %>


<%= render layout: "shared/forms/fields/layout", locals: locals do %>
<% content_for(:field) do %>
<%= form.email_field :login,
placeholder: locals[:placeholder],
class: "form-control g-py-15 g-px-15",
"data-error-class"=>"u-has-error-v1-3",
"data-success-class"=>"u-has-success-v1-2",
"data-msg" => t('forms.shared.required'),
autofocus: locals[:autofocus],
required: locals[:required] %>
<% end %>
<% end %>

当我这样做时:

<%= render "shared/forms/fields/email", locals: {form: f} %>

<%= render "shared/forms/fields/login", locals: {form: f} %>

我明白了

Email Field

Email Field/Login Field

我发现 content_for 会“追加”您提供的 block ,然后当我yield 时返回整个 block 。

第一次 content_for(:field) 中没有任何内容,它附加到 Email Field。但是第二次它并没有清除它的内容,只是将 Login Field 附加到它。

我正在考虑为 layout.html.erb 添加额外的复杂性,所以只保持内联不是一个选项。

有没有办法告诉布局只产生 content_for 的“最新”值。

编辑:

我写了一个在 yield 之后刷新的方法,提示将再次使用相同的 key :

def yield_and_flush!(content_key)
view_flow.content.delete(content_key)
end

最佳答案

content_forflush 选项来重置以前的内容:

<% content_for :field, flush: true do %>
new content here
<% end %>

关于html - Rails 使用 content_for 并渲染相同的布局两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58821250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com