开发者

Extract to Rails helper

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 %>
<% e开发者_如何学Cnd %>


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:

def language_tabs(languages, &block) 
  tabs = []
  languages.each_with_index do |lang, i|
    tabs << render(:partial => 'tab', :locals => {:body => capture(lang, &block), :lang => lang, :i => i })
  end

  render(:partial => 'langtabs', :locals => {:languages => languages, :tabs => tabs})

end 

2) Partial _langtabs.html.erb

<div class="tabs">
  <ul>
  <% languages.each_with_index do |lang, i| %>
    <li><a href="#tabs-<%= i+1 %>"><%= lang %></a></li>
  <% end %>
  </ul>

  <% tabs.each do |cTab| %>
    <%= cTab %>
  <% end %>

</div>

3) partial _tab.html.erb

<div id="tabs-<%= i+1 %>">
<fieldset class="inputs">
<ol>
  <%= body %>
</ol>
</fieldset>
</div>

4) Your actual form (note: there's a variable called @languages in my example!) :

<%= form_for :page, :url => { :action => :create } do |form| %>
  <% language_tabs @languages 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 %>

Grüsse

Simon


I'm not sure if this is exactly what you want but here's another suggestion:

Rails I18n: http://guides.rubyonrails.org/i18n.html

You don't have to make a helper for this, just separate yml files to translate for each language.


Check Cells. You can write isolated (if needed), encapsulated and reusable components with theirs own logic in controller-like clasess (but much lighter). Works well with Rails 3.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜