diff options
author | Yves Senn <yves.senn@gmail.com> | 2017-03-26 11:42:39 +0200 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2017-03-26 12:11:45 +0200 |
commit | 27f103fc7e3260efe0b8dde66bf5354f2202ee32 (patch) | |
tree | ef196c56123ccb0859830c39b74b6458250d0bcf /railties | |
parent | d96dde82b7ff3a216c16eb87e2d346341ad53c05 (diff) | |
download | rails-27f103fc7e3260efe0b8dde66bf5354f2202ee32.tar.gz rails-27f103fc7e3260efe0b8dde66bf5354f2202ee32.tar.bz2 rails-27f103fc7e3260efe0b8dde66bf5354f2202ee32.zip |
add field ids when generating a scaffold form.
This is a follow up to a6d065e. When using `form_with` you must supply
field ids manually. Since the scaffold generator is using labels we
need to make sure that they are linked up properly.
Diffstat (limited to 'railties')
-rw-r--r-- | railties/CHANGELOG.md | 9 | ||||
-rw-r--r-- | railties/lib/rails/generators/erb/scaffold/templates/_form.html.erb | 6 | ||||
-rw-r--r-- | railties/lib/rails/generators/named_base.rb | 4 | ||||
-rw-r--r-- | railties/test/generators/scaffold_generator_test.rb | 8 |
4 files changed, 20 insertions, 7 deletions
diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index 6032d2e1a1..46bb0fbc91 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -1 +1,10 @@ Please check [5-1-stable](https://github.com/rails/rails/blob/5-1-stable/railties/CHANGELOG.md) for previous changes. + + +* Specify form field ids when generating a scaffold. + + This makes sure that the labels are linked up with the fields. The + regression was introduced when the template was switched to + `form_with`. + + *Yves Senn* diff --git a/railties/lib/rails/generators/erb/scaffold/templates/_form.html.erb b/railties/lib/rails/generators/erb/scaffold/templates/_form.html.erb index 73c00ad41a..4f2e84f924 100644 --- a/railties/lib/rails/generators/erb/scaffold/templates/_form.html.erb +++ b/railties/lib/rails/generators/erb/scaffold/templates/_form.html.erb @@ -15,15 +15,15 @@ <div class="field"> <% if attribute.password_digest? -%> <%%= form.label :password %> - <%%= form.password_field :password %> + <%%= form.password_field :password, id: :<%= field_id(:password) %> %> </div> <div class="field"> <%%= form.label :password_confirmation %> - <%%= form.password_field :password_confirmation %> + <%%= form.password_field :password_confirmation, id: :<%= field_id(:password_confirmation) %> %> <% else -%> <%%= form.label :<%= attribute.column_name %> %> - <%%= form.<%= attribute.field_type %> :<%= attribute.column_name %> %> + <%%= form.<%= attribute.field_type %> :<%= attribute.column_name %>, id: :<%= field_id(attribute.column_name) %> %> <% end -%> </div> diff --git a/railties/lib/rails/generators/named_base.rb b/railties/lib/rails/generators/named_base.rb index 02557b098a..46001f306a 100644 --- a/railties/lib/rails/generators/named_base.rb +++ b/railties/lib/rails/generators/named_base.rb @@ -149,6 +149,10 @@ module Rails "new_#{singular_table_name}_url" end + def field_id(attribute_name) + [singular_table_name, attribute_name].join('_') + end + def singular_table_name # :doc: @singular_table_name ||= (pluralize_table_names? ? table_name.singularize : table_name) end diff --git a/railties/test/generators/scaffold_generator_test.rb b/railties/test/generators/scaffold_generator_test.rb index 0e0ef7d293..bc76cead18 100644 --- a/railties/test/generators/scaffold_generator_test.rb +++ b/railties/test/generators/scaffold_generator_test.rb @@ -437,8 +437,8 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase end assert_file "app/views/accounts/_form.html.erb" do |content| - assert_match(/^\W{4}<%= form\.text_field :name %>/, content) - assert_match(/^\W{4}<%= form\.text_field :currency_id %>/, content) + assert_match(/^\W{4}<%= form\.text_field :name, id: :account_name %>/, content) + assert_match(/^\W{4}<%= form\.text_field :currency_id, id: :account_currency_id %>/, content) end end @@ -461,8 +461,8 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase end assert_file "app/views/users/_form.html.erb" do |content| - assert_match(/<%= form\.password_field :password %>/, content) - assert_match(/<%= form\.password_field :password_confirmation %>/, content) + assert_match(/<%= form\.password_field :password, id: :user_password %>/, content) + assert_match(/<%= form\.password_field :password_confirmation, id: :user_password_confirmation %>/, content) end assert_file "app/views/users/index.html.erb" do |content| |