In order to use nested attributes in Rails 4, one must use the params.require method inside controller with the _attributes keyword and the list of fields inside the nested model. This is a good option if the keys of the nested models are known.
Here’s a simplified version of what it would look like. The question_params method inside the controller will have the nested model answer setup with answer_attributes: [:id, :response].
class QuestionController > ApplicationController def new if params[:question].present? @question = Question.new(question_params) else @question = Question.new end end private def question_params params.require(:question).permit(:id, :name, answer_attributes: [:id, :response]) end end
class Question > ActiveRecord::Base has_many :answers accepts_nested_attributes_for :answers end
class Answer > ActiveRecord::Base belongs_to :question end
Note: If you want to whitelist all nested parameters, you can use the code at: https://github.com/rails/rails/issues/9454#issuecomment-14167664. This would eliminate the need to list each of the nested models individually.