Backbone and Rails associations: Avoiding JSON HashWithIndifferentAccess errors

后端 未结 2 1236
一整个雨季
一整个雨季 2021-02-01 00:08

I\'m trying to get my backbone associations working inside a rails app , and I\'m having difficulty when trying to update existing models. Specifically, Rails throws the followi

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-01 00:24

    For those who are Googling...

    While the current accepted answer certainly works, the following was how I worked out my problem (very similar, but slightly different):

    I was using a nested form, with models nested 4 layers deep. In my controller which was rendering the form, I needed to build out the form.

    def new
      @survey = Survey.new
      3.times do
        question = @survey.questions.build
        4.times { question.answers.build }
      end
    end
    

    This allowed the params to include params[:survey][:questions_attributes]. Rails renamed the parameters for me because I informed it ahead of time.

    Hope this helps!

提交回复
热议问题