diff options
author | Genadi Samokovarov <gsamokovarov@gmail.com> | 2015-12-13 19:37:22 +0100 |
---|---|---|
committer | Genadi Samokovarov <gsamokovarov@gmail.com> | 2015-12-13 20:18:19 +0100 |
commit | c83ace7b6a5720be30a2f7d843d545fc81af54cd (patch) | |
tree | 2084a99088604dc00be5ea89be544caf94099273 | |
parent | eb0e8e216fcf535a1e6b82720dfb7639fcc20ff2 (diff) | |
download | rails-c83ace7b6a5720be30a2f7d843d545fc81af54cd.tar.gz rails-c83ace7b6a5720be30a2f7d843d545fc81af54cd.tar.bz2 rails-c83ace7b6a5720be30a2f7d843d545fc81af54cd.zip |
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.
3 files changed, 3 insertions, 3 deletions
diff --git a/railties/lib/rails/generators/rails/scaffold_controller/templates/api_controller.rb b/railties/lib/rails/generators/rails/scaffold_controller/templates/api_controller.rb index 17a86f376b..400afec6dc 100644 --- a/railties/lib/rails/generators/rails/scaffold_controller/templates/api_controller.rb +++ b/railties/lib/rails/generators/rails/scaffold_controller/templates/api_controller.rb @@ -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 -%> diff --git a/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb b/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb index f73e9a96ba..42b9e34274 100644 --- a/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb +++ b/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb @@ -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 -%> diff --git a/railties/test/generators/scaffold_controller_generator_test.rb b/railties/test/generators/scaffold_controller_generator_test.rb index 95ef853a11..b4ba101017 100644 --- a/railties/test/generators/scaffold_controller_generator_test.rb +++ b/railties/test/generators/scaffold_controller_generator_test.rb @@ -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 |