Ruby on Rails传递对yield的引用(模板) [英] Ruby on Rails pass reference to yield (template)
问题描述
简而言之:我的每个标签都有自己的形式,所以我决定制作一个单一的布局,只是将表单本身作为布局的可变内容.
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.
但是我需要在布局中使用 form_for ,而不是在每个表单中都使用它,因为在布局中我还有一些其他常见的表单元素.
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.
那么如何将表单生成器的引用 f 传递给模板?
So how can I pass the form builder's reference f to the template ?
布局代码:
<% content_for(:content) do %>
<%= form_for current_form do |f| %>
<%= yield %>
<%= f.submit "Submit" %>
<% end %>
<% end %>
有可能吗?
PS发现了这一点:干一个助手:包装form_for并访问本地表单变量(@rubish的答案),但是<%= yield f %>
似乎不起作用,对于该视图f仍未定义.
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.
推荐答案
为什么不为标签创建通用模板( not 布局),并使用 partial 每个标签内容的模板?
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} %>
这篇关于Ruby on Rails传递对yield的引用(模板)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!