Unpermitted parameters for double-nested models in Rails 4

后端 未结 1 1080
抹茶落季
抹茶落季 2020-12-14 23:20

I\'m new to Rails and built something based on this, but it needed small updates to make it compatible with Rails 4\'s strong parameters:

http://railscasts.com/episo

相关标签:
1条回答
  • 2020-12-14 23:44

    I found out the fix is that the whitelist needs to match the nesting of attributes. So to fix the above I changed this:

    class SurveysController < ApplicationController
      def survey_params
        params.require(:survey).permit(:name, questions_attributes: [:id, :survey_id,    :content])
      end
    

    to this:

    class SurveysController < ApplicationController
      def survey_params
        params.require(:survey).permit(:name, questions_attributes: [:id, :survey_id,    :content, answers_attributes: [:id, :question_id, :content]])
      end
    

    E.g. simply copy the whitelist of the answers_attributes and insert it inside before the closing "]" for the questions_attributes.

    Hopefully this will help others with the same problem.

    0 讨论(0)
提交回复
热议问题