2

在 Rails 3 中搭建脚手架时,生成器会执行以下操作:

<% if @user.errors.any? %>
<div id="error_explanation">
  <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>

  <ul>
  <% @user.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
  <% end %>
  </ul>
</div>
<% end %>

但是 i18n 呢?消息被翻译,但为什么是硬编码的标题?我应该手动翻译吗?这似乎不是很干?

问候,

雅各布

4

2 回答 2

2

您可以通过在 github 上下载文件并将 em 放在您的lib/templates/erb/scaffold. 当您生成脚手架时,它会意识到模板在那里并使用它们。

<% this code is executed and probably result in html %>, <%% this is converted to snippet %>,这并不难,然后您可以执行所有个性化设置,但是yml自动生成文件可能更难,无论如何您仍然可以几乎自动完成模板。

为了回答您的问题,我也认为脚手架可以改进,但我阅读了开发人员喜欢从头开始编写自己的代码的所有地方。因此,我认为,努力的重点是改进其他功能。当您是初学者时,Scaffold 至少仍然是一个有用的生成器,可以帮助您理解和熟悉逻辑。

于 2011-02-05T01:59:25.513 回答
0

I think that scaffolding is more a hint than a final template.

you may want to add translation simply substituing "prohibited this user from being saved:" with t('my_message') and dding 'my_message: whatever' to the en.yml. Adding a translation you have no repetitions and the dry principle is safe.

Just a comment aside: I seldomly use a message like "4 errors prohibited this user from being saved". In my experience is more common just to list all the errors occurred and to display the wrong field in a kind of red.

于 2011-02-02T08:21:59.243 回答