提取到 Rails 助手
我想让这个东西变得更通用。
<%= 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来是部分模板的良好候选者。 http://api.rubyonrails.org/classes/ActionView/Partials.html
我刚刚意识到你真正想要什么:) 所以你可以结合部分和块助手,看看:
http://snippets.dzone.com/posts/show/2483
我将在 10 分钟内发布解决方案;)
这就是您的解决方案:
1)
application_helper.rb
(或任何其他合适的帮助文件)添加以下内容:2)部分
_langtabs.html.erb
3)部分
_tab.html.erb
4)您的实际表单(注意:在我的示例中有一个名为 @languages 的变量!):
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:2) Partial
_langtabs.html.erb
3) partial
_tab.html.erb
4) Your actual form (note: there's a variable called @languages in my example!) :
Grüsse
Simon
我不确定这是否正是您想要的,但这里有另一个建议:
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.
检查单元格。您可以在类似控制器的类(但更轻)中编写具有自己的逻辑的隔离(如果需要)、封装和可重用的组件。
与 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.