Skip to content

Commit 2dc6203

Browse files
committed
Fix generation of broken controller on Rails 5.0 when ran w/o fields
1 parent b090db1 commit 2dc6203

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

lib/generators/rails/templates/api_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def set_<%= singular_table_name %>
5454
# Never trust parameters from the scary internet, only allow the white list through.
5555
def <%= "#{singular_table_name}_params" %>
5656
<%- if attributes_names.empty? -%>
57-
params[<%= ":#{singular_table_name}" %>]
57+
params.fetch(<%= ":#{singular_table_name}" %>, {})
5858
<%- else -%>
5959
params.require(<%= ":#{singular_table_name}" %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>)
6060
<%- end -%>

lib/generators/rails/templates/controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def set_<%= singular_table_name %>
7575
# Never trust parameters from the scary internet, only allow the white list through.
7676
def <%= "#{singular_table_name}_params" %>
7777
<%- if attributes_names.empty? -%>
78-
params[<%= ":#{singular_table_name}" %>]
78+
params.fetch(<%= ":#{singular_table_name}" %>, {})
7979
<%- else -%>
8080
params.require(<%= ":#{singular_table_name}" %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>)
8181
<%- end -%>

test/scaffold_api_controller_generator_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,14 @@ class ScaffoldApiControllerGeneratorTest < Rails::Generators::TestCase
4242
assert_match(/params\.require\(:post\)\.permit\(:title, :body\)/, content)
4343
end
4444
end
45+
46+
test 'dont use require and permit if there are no attributes' do
47+
run_generator %w(Post --api)
48+
49+
assert_file 'app/controllers/posts_controller.rb' do |content|
50+
assert_match(/def post_params/, content)
51+
assert_match(/params\.fetch\(:post, {}\)/, content)
52+
end
53+
end
4554
end
4655
end

test/scaffold_controller_generator_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,13 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
5454
assert_match(/params\.require\(:post\)\.permit\(:title, :body\)/, content)
5555
end
5656
end
57+
58+
test 'dont use require and permit if there are no attributes' do
59+
run_generator %w(Post)
60+
61+
assert_file 'app/controllers/posts_controller.rb' do |content|
62+
assert_match(/def post_params/, content)
63+
assert_match(/params\.fetch\(:post, {}\)/, content)
64+
end
65+
end
5766
end

0 commit comments

Comments
 (0)