From f4be8f13286a02c834f905d42b239e2134005ddc Mon Sep 17 00:00:00 2001 From: Anton Bangratz Date: Fri, 1 Oct 2010 18:10:02 +0200 Subject: [PATCH 2/3] Human attribute names for nested labels Labels try to look up the text in the i18n backend via the singularized name of the object the attributes are nested for. --- actionpack/lib/action_view/helpers/form_helper.rb | 2 +- actionpack/test/template/form_helper_test.rb | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletions(-) diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 1eee04c..9ca7986 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -884,7 +884,7 @@ module ActionView else content = if text.blank? if object_name =~ /.+\[(.+)_attributes\]/ then - base_name = $1 + base_name = $1.singularize I18n.t("helpers.label.#{base_name}.#{method_name}", :default => "").presence else I18n.t("helpers.label.#{object_name}.#{method_name}", :default => "").presence diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index 60334e6..7128eea 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -61,6 +61,9 @@ class FormHelperTest < ActionView::TestCase :label => { :author => { :name => "Author Name" + }, + :comment => { + :name => "Comment Name" } } } @@ -1003,6 +1006,24 @@ class FormHelperTest < ActionView::TestCase I18n.locale = old_locale end + def test_form_for_with_nested_fields_for_plurals_and_humanized_label_text + @post.comments = Array.new(1) { |id| Comment.new(id + 1) } + old_locale, I18n.locale = I18n.locale, :nested + output_buffer = form_for(@post) do |f| + @post.comments.each do |comment| + concat f.fields_for(:comments, comment) { |c| + concat c.label(:name) + } + end + end + expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => :put) do + "" + end + assert_dom_equal expected, output_buffer + ensure + I18n.locale = old_locale + end + def test_nested_fields_for_with_index_on_both form_for(@post, :index => 1) do |f| concat f.fields_for(:comment, @post, :index => 5) { |c| -- 1.7.1