Skip to content

Commit

Permalink
add field ids when generating a scaffold form.
Browse files Browse the repository at this point in the history
Backport of 27f103f

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 765d6db commit 9d69b69
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
8 changes: 8 additions & 0 deletions railties/CHANGELOG.md
@@ -1,3 +1,11 @@
* 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*

## Rails 5.1.0.rc1 (March 20, 2017) ##

* Add `app:update` task to engines.
Expand Down
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
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
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

2 comments on commit 9d69b69

@senny
Copy link
Member Author

@senny senny commented on 9d69b69 Mar 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/cc @kaspth

@kaspth
Copy link
Contributor

@kaspth kaspth commented on 9d69b69 Mar 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet 👍

Please sign in to comment.