Skip to content

Commit ab3f7b2

Browse files
rubysclaude
andcommitted
Fix: Questions not being saved due to strong parameters issue
Fixed critical bug where questions were not being saved to the database. Root cause: Rails 8's params.expect() doesn't properly handle nested attributes with dynamic hash keys (timestamps used for new records). The questions_attributes hash was being filtered out entirely. Solution: Changed billable_params from params.expect() to the traditional params.require().permit() pattern, which correctly handles nested attributes with arbitrary keys. Also fixed: - Added reject_if: proc to accepts_nested_attributes_for to skip questions with blank question_text - Updated process_question_params to set empty choices to nil instead of empty array for textarea types All 949 tests passing. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent ae1228c commit ab3f7b2

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

app/controllers/billables_controller.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,9 @@ def set_billable
220220

221221
# Only allow a list of trusted parameters through.
222222
def billable_params
223-
params.expect(billable: [:type, :name, :price, :order, :couples, :table_size, { options: {} }, { packages: {} },
224-
{ questions_attributes: [:id, :question_text, :question_type, :choices, :order, :_destroy] }])
223+
params.require(:billable).permit(:type, :name, :price, :order, :couples, :table_size,
224+
options: {}, packages: {},
225+
questions_attributes: [:id, :question_text, :question_type, :choices, :order, :_destroy])
225226
end
226227

227228
def update_includes
@@ -263,7 +264,8 @@ def process_question_params(params)
263264
if question_attrs[:choices].is_a?(String)
264265
# Convert newline-separated text to array
265266
choices_array = question_attrs[:choices].split("\n").map(&:strip).reject(&:blank?)
266-
params[:questions_attributes][key][:choices] = choices_array
267+
# Set to nil for textarea types (empty array), otherwise use the array
268+
params[:questions_attributes][key][:choices] = choices_array.empty? ? nil : choices_array
267269
end
268270
end
269271

app/models/billable.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ class Billable < ApplicationRecord
2222
has_many :tables, foreign_key: :option_id, dependent: :destroy
2323
has_many :questions, dependent: :destroy
2424

25-
accepts_nested_attributes_for :questions, allow_destroy: true
25+
accepts_nested_attributes_for :questions, allow_destroy: true,
26+
reject_if: proc { |attributes| attributes['question_text'].blank? }
2627

2728
def people
2829
if type == 'Option'

0 commit comments

Comments
 (0)