mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Prevent a 500 in the default controller scaffold
If you update a record with no attributes, you'll hit a 500 from a guard in `assign_attributes` raising: ```ruby ArgumentError: When assigning attributes, you must pass a hash as an argument. app/controllers/users_controller.rb:44:in `block in update' app/controllers/users_controller.rb:43:in `update' test/controllers/users_controller_test.rb:37:in `block in <class:UsersControllerTest>' ``` Not a biggie, but may be quite confusing for the folks new to the framework.
This commit is contained in:
parent
eb0e8e216f
commit
c83ace7b6a
3 changed files with 3 additions and 3 deletions
|
@ -52,7 +52,7 @@ class <%= controller_class_name %>Controller < ApplicationController
|
|||
# Only allow a trusted parameter "white list" through.
|
||||
def <%= "#{singular_table_name}_params" %>
|
||||
<%- if attributes_names.empty? -%>
|
||||
params[:<%= singular_table_name %>]
|
||||
params.fetch(:<%= singular_table_name %>, {})
|
||||
<%- else -%>
|
||||
params.require(:<%= singular_table_name %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>)
|
||||
<%- end -%>
|
||||
|
|
|
@ -59,7 +59,7 @@ class <%= controller_class_name %>Controller < ApplicationController
|
|||
# Only allow a trusted parameter "white list" through.
|
||||
def <%= "#{singular_table_name}_params" %>
|
||||
<%- if attributes_names.empty? -%>
|
||||
params[:<%= singular_table_name %>]
|
||||
params.fetch(:<%= singular_table_name %>, {})
|
||||
<%- else -%>
|
||||
params.require(:<%= singular_table_name %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>)
|
||||
<%- end -%>
|
||||
|
|
|
@ -56,7 +56,7 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
|
|||
|
||||
assert_file "app/controllers/users_controller.rb" do |content|
|
||||
assert_match(/def user_params/, content)
|
||||
assert_match(/params\[:user\]/, content)
|
||||
assert_match(/params\.fetch\(:user, \{\}\)/, content)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue