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?

4
  • Could you share request that Angular doing? Commented Nov 4, 2014 at 21:01
  • Do you mean the http post itself? Commented Nov 5, 2014 at 15:13
  • Yes,from web-inspector. Commented Nov 5, 2014 at 19:46
  • Just added, the header and the submitted data. Commented Nov 6, 2014 at 15:43

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

What about the base params? It created to compensate the lack of rule key?
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.
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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.