Ruby on Rails pass reference to yield (template)

不想你离开。 提交于 2019-12-10 19:12:29

问题


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.

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

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.


回答1:


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} %>


来源:https://stackoverflow.com/questions/12598203/ruby-on-rails-pass-reference-to-yield-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!