I want to make this thing more generic.
<%= form_for :page, :url => { :action => :create } do |form| %>
<div class="tabs">
<ul>
<% languages.each_with_index do |lang, i| %>
<li><a href="#tabs-<%= i+1 %>"><%= lang %></a></li>
<% end %>
</ul>
<% languages.each_with_index do |lang, i| %>
<div id="tabs-<%= i+1 %>">
<fieldset class="inputs">
<ol>
<li><%= form.label fname("title", lang), "Title" %>
<%= form.text_field fname("title", lang), :size => 80, :class => "text" %></li>
<li><%= form.text_area fname("content", lang), :cols => 40, :rows => 10 %></li>
<li><%= form.label fname("published", lang), "Publish?" %>
<%= form.check_box fname("published", lang) %></li>
</ol>
</fieldset>
</div>
<% end %>
</div>
It generates a tab and form-fields for each language. I need this tabs for other forms too, so I want to extract the “logic” into a helper. Problem is, that I dont know how.
<%= form_for :page, :url => { :action => :create } do |form| %>
<% language_tabs do |lang| %>
<li><%= form.label fname("title", lang), "Title" %>
<%= form.text_field fname("title", lang), :size => 80, :class => "text" %></li>
<li><%= form.text_area fname("content", lang), :cols => 40, :rows => 10 %></li>
<li><%= form.label fname("published", lang), "Publish?" %>
<%= form.check_box fname("published", lang) %></li>
<% end %>
<% end %>
looks like a good candidate for a partial template. http://api.rubyonrails.org/classes/ActionView/Partials.html
I just realized what you really want 🙂 So you can combine partials and block helpers, look at:
http://snippets.dzone.com/posts/show/2483
I’ll post a solution in 10mins 😉
So here comes your solution:
1)
application_helper.rb(or any other suitable helper file) add this:2) Partial
_langtabs.html.erb3) partial
_tab.html.erb4) Your actual form (note: there’s a variable called @languages in my example!) :
Grüsse
Simon