KiT O
KiT O

Reputation: 867

Rails wrap_parameters weired behavior, working on one controller and not working on another

I have two different controller, both inherited from ApplicationController, class PagesController < ApplicationController and class CommentsController < ApplicationController.

Posting on page#test copy all posted params to another hash attribute :

# POST '/pages/test' :
{ "awd":"awd" }

# result :
{
   "awd": "awd",
   "action": "test_post",
   "controller": "pages",
   "page": {
       "awd": "awd"
   }
}

# POST '/comments' :
{"awd":"awd"}

# result :
{
    "awd": "awd",
    "action": "create",
    "controller": "comments",
    "comment": {}
}

Comment routes create by resources :comments, only: [:create], post '/comments' => 'comments#create' do the same.

P.S. : No before_filter or any extra code in any of them, all requests are json, wrap_parameter.rb has wrap_parameters format: [:json].

Edit

There's just one difference between those controllers, CommentsController generated with rails g scaffold ... and PagesController generated with rails g controller ...

Edit 2

self._wrapper_options in CommentsController self._wrapper_options has value of

{
    "format": [
        "json"
    ],
    "include": [
        "_id",
        "created_at",
        "updated_at",
        "content",
    ],
    "name": "comment"
}

That made the problem, but why when I didn't set include in wrap_parameters, rails added that?

Upvotes: 3

Views: 2192

Answers (1)

phylae
phylae

Reputation: 1001

On ActiveRecord models with no :include or :exclude option set, it will only wrap the parameters returned by the class method attribute_names.

http://api.rubyonrails.org/classes/ActionController/ParamsWrapper.html

I'm guessing that "awd" is an attribute on the Page model, but not on the Comment model. If you really want to send that JSON to your comments controller, try adding something like this:

wrap_parameters :comment, include: [:awd]

Upvotes: 3

Related Questions