提取到 Rails 助手

发布于 2024-09-19 10:11:49 字数 1641 浏览 7 评论 0原文

我想让这个东西变得更通用。

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

它为每种语言生成一个选项卡和表单字段。我也需要其他表单的选项卡,所以我想将“逻辑”提取到帮助程序中。问题是,我不知道怎么做。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷你仙 2024-09-26 10:11:49

看起来是部分模板的良好候选者。 http://api.rubyonrails.org/classes/ActionView/Partials.html

我刚刚意识到你真正想要什么:) 所以你可以结合部分和块助手,看看:

http://snippets.dzone.com/posts/show/2483

我将在 10 分钟内发布解决方案;)

这就是您的解决方案:

1) application_helper.rb(或任何其他合适的帮助文件)添加以下内容:

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)部分_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)部分_tab.html.erb

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

4)您的实际表单(注意:在我的示例中有一个名为 @languages 的变量!):

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

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

好久不见√ 2024-09-26 10:11:49

我不确定这是否正是您想要的,但这里有另一个建议:

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

您不必为此创建一个助手,只需为每种语言翻译单独的 yml 文件即可。

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.

迷迭香的记忆 2024-09-26 10:11:49

检查单元格。您可以在类似控制器的类(但更轻)中编写具有自己的逻辑的隔离(如果需要)、封装和可重用的组件。
与 Rails 3 配合良好。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文