0

I've been ripping my hair off with this one. I have read all the doc about Rails 4 integrating strong params and that now everything has to be explicitly whitelisted. But it still won't go through!!!

Here is my setup

Models

class Course < ActiveRecord::Base
  has_many :chapters
  accepts_nested_attributes_for :chapters
end

class Chapter < ActiveRecord::Base
  belongs_to :course
end

Controller

class CoursesController < ApplicationController
  respond_to :json

  def create
    @course = Course.create permitted_params
    respond_with @course
  end

  private

  def permitted_params
    params.require(:course).permit(:name, chapters_attributes: [:title, :content])
  end
end

JSON from client

{
    "course": {
        "chapters": [{
            "title": "qwerty",
            "content": "foobar"
        }],
        "name": "Test course"
    }
}

Server log

Started POST "/json/courses" for 10.0.2.2 at 2014-02-24 15:29:44 +0000
Processing by CoursesController#create as JSON
  Parameters: {"course"=>{"chapters"=>[{"title"=>"qwerty", "content"=>"foobar"}], "name"=>"Test course"}}
Unpermitted parameters: chapters
Completed 201 Created in 96ms (Views: 52.1ms | ActiveRecord: 4.1ms)

Unpermitted params: chapters. I've been staring at this for hours with no avail. I honestly don't know what I'm doing wrong. Please tell me its right there and I just forgot some stupid magical param so I can move on.

bjhaid
  • 9,592
  • 2
  • 37
  • 47
Abe Dadoun
  • 145
  • 1
  • 10

2 Answers2

2

I believe you just need to change to 'chapters' in your permitted_params method :

def permitted_params
    params.require(:course).permit(:name, chapters: [:title, :content])
end

instead of "chapters_attributes"

SciPhi
  • 2,585
  • 1
  • 18
  • 19
  • Or make the json return `chapters_attributes` ... one way or another, you have a mismatch there. I think rails nested attributes wants the nested attributes to be `chapters_attributes` – DGM Feb 25 '14 at 01:22
  • This does not change the result. It refuses to accept chapters. Besides this is the new convention: http://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html – Abe Dadoun Feb 25 '14 at 01:38
  • When I change it to chapters, I get a AssociationTypeMismatch exception. – Abe Dadoun Feb 25 '14 at 01:53
1

I believe the issue is not in the controller or model, but in the JSON sent in the request.

Processing by CoursesController#create as JSON
Parameters: {"course"=>{"chapters"=>[{"title"=>"qwerty", "content"=>"foobar"}], "name"=>"Test course"}}

should instead be

Processing by CoursesController#create as JSON
Parameters: {"course"=>{"chapters_attributes"=>[{"title"=>"qwerty", "content"=>"foobar"}], "name"=>"Test course"}}

If you post your view code we can probably track down the issue fairly quickly.

  • 1
    Oh god! You made me realize I had misspelled one of my other params. I had tried this before and it failed, but it was for another reason and now that I changed it again I saw it was because of that typo! Thanks – Abe Dadoun Feb 25 '14 at 02:11