开发者

Rails 3 scaffold and i18n

When scaffolding in Rails 3 the generator does this:

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

But what about i18n? The messages get translated but why开发者_开发技巧 the hardcoded headline? Am I supposed to translate that manually? That doesn't seem very DRY?

Regards,

Jacob


you can override scaffold default behavior by downloading the files here on github and place em in your lib/templates/erb/scaffold. When you generate scaffold it realize the templates are there and use those.

<% this code is executed and probably result in html %>, <%% this is converted to snippet %>, it was not that difficult and then you can perform all your personalizations, but to have the yml file automatically generated could be harder, anyway you can still get your templates almost done automatically.

To answer your question I do too think scaffold could be improved, but I read everywhere developers prefer to write their own code from scratch. Therefor I think, efforts are focused on improving other features. Scaffold remains at the very least a useful generator to understand and get familiar with the logic when you're a beginner.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜