Skip to content

Commit

Permalink
Revert params.fetch to params.expect conversions in scaffold
Browse files Browse the repository at this point in the history
The conversions have the unintended consequence of requiring
params when none are expected on the default generated templates.

Originally pointed out in rails/jbuilder#573.
  • Loading branch information
martinemde committed Sep 15, 2024
1 parent 6e46b42 commit c9f9772
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class <%= controller_class_name %>Controller < ApplicationController
# Only allow a list of trusted parameters through.
def <%= "#{singular_table_name}_params" %>
<%- if attributes_names.empty? -%>
params.expect(<%= singular_table_name %>: {})
params.fetch(:<%= singular_table_name %>, {})
<%- else -%>
params.expect(<%= singular_table_name %>: [ <%= permitted_params %> ])
<%- end -%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class <%= controller_class_name %>Controller < ApplicationController
# Only allow a list of trusted parameters through.
def <%= "#{singular_table_name}_params" %>
<%- if attributes_names.empty? -%>
params.expect(<%= singular_table_name %>: {})
params.fetch(:<%= singular_table_name %>, {})
<%- else -%>
params.expect(<%= singular_table_name %>: [ <%= permitted_params %> ])
<%- end -%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_dont_use_require_or_permit_if_there_are_no_attributes

assert_file "app/controllers/users_controller.rb" do |content|
assert_match(/def user_params/, content)
assert_match(/params\.expect\(user: \{\}\)/, content)
assert_match(/params\.fetch\(:user, \{\}\)/, content)
end
end

Expand Down

0 comments on commit c9f9772

Please sign in to comment.