user3758418
user3758418

Reputation: 51

rails does not wrap all paramters passed

I have two models in rails
1. list

   class List < ActiveRecord::Base
                has_many:list_hd_channel
    end

2 listhdchannel

class ListHdChannel < ActiveRecord::Base
          belongs_to :list
    end

My config/routes file looks like this

namespace :api do
        resources :eagles, :defaults => {:format => 'json'}
        resources :daily13_channel,  :defaults => {:format => 'json'}
        resources :lists,  :defaults => {:format => 'json'}
end

The problem is when send do a PUT request (json) with any parameters, the parameter that I get at controller are not wrapped under the key list. But if I send a parameter which is a column of the model "list" then it is wrapped. REQUEST

http://ip:3000/api/lists/1
{  
            "list_name": 1
}

Server side logs

Started PUT "/api/lists/1" for address at 2014-06-19 17:32:01 -0700
Processing by Api::ListsController#update as JSON
Parameters: {"list_name"=>1, "id"=>"1", "list"=>{"list_name"=>1}}

REQUEST

http://address:3000/api/lists/1
{  
            "any_param": 1
}

Server Side Logs

Started PUT "/api/lists/1" for address at 2014-06-19 17:34:02 -0700
Processing by Api::ListsController#update as JSON
Parameters: {"any_param"=>1, "id"=>"1", "list"=>{}}

As you can see the server side logs do not wrap the "any_param" inside "list", even if the url is for the list controller. Can any one help me understand why is it not wrapping any parameter that I pass and just wraps the one (list_name) which is a column in the list model?

Upvotes: 3

Views: 954

Answers (1)

user3758418
user3758418

Reputation: 51

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

The last point here explains that rails by default is trying to check if a model with same name as that of controller exists; and if yes, then it uses the wrapper key for the model/controller name and wraps the parameters which belong to that model only.

Upvotes: 2

Related Questions