Ruby on Rails传递对yield的引用(模板)

时间:2022-11-14 05:35:52

To make long story short: each of my tabs has its own form, so I decided to make a single layout and just to have a forms themselves as a variable content for a layout.

长话短说:我的每个选项卡都有自己的表单,所以我决定创建一个单独的布局,并将表单本身作为一个布局的可变内容。

But I need to have form_for to be in a layout, rather then having it in each of the forms, because I have some other common form elements in the layout.

但是我需要在布局中有form_for,而不是在每个表单中都有它,因为我在布局中还有其他一些常见的表单元素。

So how can I pass the form builder's reference f to the template ?

那么如何将form builder的引用f传递给模板呢?

Layout code:

布局代码:

<% content_for(:content) do %>
  <%= form_for current_form do |f| %>
    <%= yield %>
    <%= f.submit "Submit" %>
  <% end %>
<% end %>

Is it possible ?

是可能的吗?

P.S Found this one: DRYing up a helper: wrap form_for and access local form variable (@rubish's answer), but <%= yield f %> doesn't seem to be working, f still remains undefined for the view.

P。我们找到了这一项:将一个帮助器:wrap form_for和访问本地表单变量(@rubish的答案),但是<%= yield f %>似乎没有作用,f仍然没有为视图定义。

1 个解决方案

#1


2  

Why don't you make a common template (not layout) for the tabs, and use a partial template for the content of each tab?

为什么不为选项卡创建一个通用模板(而不是布局),并为每个选项卡的内容使用一个部分模板?

Then you can do something like:

然后你可以这样做:

<%= render :partial => @tab_name, :locals => {:form => f} %>

#1


2  

Why don't you make a common template (not layout) for the tabs, and use a partial template for the content of each tab?

为什么不为选项卡创建一个通用模板(而不是布局),并为每个选项卡的内容使用一个部分模板?

Then you can do something like:

然后你可以这样做:

<%= render :partial => @tab_name, :locals => {:form => f} %>