Skip to content

Commit

Permalink
add field ids when generating a scaffold form.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
senny committed Mar 26, 2017
1 parent d96dde8 commit 27f103f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
9 changes: 9 additions & 0 deletions railties/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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*
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
4 changes: 4 additions & 0 deletions railties/lib/rails/generators/named_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ def new_helper # :doc:
"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
Expand Down
8 changes: 4 additions & 4 deletions railties/test/generators/scaffold_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,8 @@ def test_scaffold_generator_belongs_to
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

Expand All @@ -461,8 +461,8 @@ def test_scaffold_generator_password_digest
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|
Expand Down

0 comments on commit 27f103f

Please sign in to comment.