Search code examples
ruby-on-railsrubyreferenceblockyield

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.

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.


Solution

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