2

When doing a ng-resource post to my Rails 4 app, rails is not adding the correct params, indeed it is adding an unexpected param called 'base' in the post.

My service:

App.factory('Rule', ['$resource', function ($resource){
  return $resource('/json/rules/:id', {id:'@id'}, {
    create: {method: 'POST'},
    update: {method: 'PUT'},
    delete: {method: 'DELETE'}
  });
}]);

Rails route:

namespace :json, defaults: {format: :json} do
    resources :rules
end

Rails controller:

class Json::RulesController < ApplicationController
  respond_to :json

  def create
    rule = Rule.new(rule_params)
    ap rule
    rule.save! if rule.valid?
    respond_with :json, rule
  end  

  def update
    rule = Rule.find(params[:id])
    rule.update(rule_params)
    respond_with rule
  end

  def rule_params
    params.require(:rule).permit(:business_id, :raw_value => [])
  end
end

When I do a post in my app this way:

$scope.rule.$update(function (){
  $location.path('/businesses/{0}'.format($stateParams['id']));
}, SharedMessage.addResponse).finally(function (){
  $rootScope.busy = false;
});

The http post itself sends the following data:

Accept:application/json, text/plain, */*
Accept-Encoding:gzip,deflate
Accept-Language:en-US,en;q=0.8,pt-BR;q=0.6,pt;q=0.4
Connection:keep-alive
Content-Length:142
Content-Type:application/json;charset=UTF-8
Cookie:hsfirstvisit=http%3A%2F%2Flocalhost%3A3000%2F%23%2Ftimeline||1402593173474; XSRF-TOKEN=GGNL6suNkGkOUEiUsEPxFE92C0AfCeLsT46GMrAJtoA%3D; __hstc=181257784.ee9ff47c4f98b78ed4d99e2ae1ee5edf.1402593173476.1415037010146.1415207051885.75; __hssrc=1; hubspotutk=ee9ff47c4f98b78ed4d99e2ae1ee5edf; _myapp_session=Uk9LS--914a262148c5784afe30087e8a5f6572fbf7d342
Host:localhost:3000
Origin:http://localhost:3000
Referer:http://localhost:3000/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36
X-CSRF-TOKEN:GGNL6suNkGkOUEiUsEPxFE92C0AfCeLsT46GMrAJtoA=
X-Requested-With:XMLHttpRequest
X-XSRF-TOKEN:GGNL6suNkGkOUEiUsEPxFE92C0AfCeLsT46GMrAJtoA=
Request Payload:{"raw_value":["from:me"], "business_id":"125", "volume":0}

The rails app, receives the following parameters:

Parameters: {"raw_value"=>["from:me"], "business_id"=>"125", "volume"=>0, "base"=>{"raw_value"=>["from:me"], "business_id"=>"125", "volume"=>0}}

the problem here relies in not receiving a object 'rule' in the params and this strange 'base' param.

Do you guys know what hits may be?

felipeclopes
  • 4,010
  • 2
  • 25
  • 35

1 Answers1

1

I don't know angularjs but let me try to answer.

the problem here relies in not receiving a object 'rule' in the params and this strange 'base' param

The strange base params is there because you have enabled wrap_parameters setting. check your config/initializer/wrap_parameters.rb. To disable it you just have to make

wrap_parameters false

Looking at this

Request Payload:{"raw_value":["from:me"], "business_id":"125", "volume":0}

and

params.require(:rule).permit(:business_id, :raw_value => [])

It just means that you are not sending params in proper way. it should be wrapped in rule i.e something

 Request Payload:{"rule": {"raw_value":["from:me"], "business_id":"125", "volume":0} }

So, make your setting in angular somewhere so it sends the params in proper way.

Paritosh Piplewar
  • 7,982
  • 5
  • 26
  • 41
  • What about the base params? It created to compensate the lack of rule key? – felipeclopes Nov 11 '14 at 16:26
  • No. it was due to `include_root_in_json` option you have in `config/initializer/wrap_parameters.rb` setting. The root name is taken from the controller which you're sending the params. – Paritosh Piplewar Nov 11 '14 at 18:47
  • Well, but the controller I'm taking the params from is the rules_controller. Why is it wrapping into base instead of rules or rule? – felipeclopes Nov 18 '14 at 03:00