Pregunta

in Rails4 when using Angular ngResource with update method 'PATCH', I get a server error:

ActionController::ParameterMissing (param not found: page):
 app/controllers/json_api/pages_controller.rb:39:in `filter_page_params'
 app/controllers/json_api/pages_controller.rb:17:in `update'

window.app.factory 'Page', ($resource) ->
  ...
ReStFull: ( ->
  return $resource('/json/pages/:id', {id: '@id'}, {update: {method: 'PATCH'}}) 
 )

Only PUT works!

My Rails4 controller action looks like this:

def update
  if @page.update_attributes(filter_page_params)
    render json: @page
  else
    render nothing: true, status: 400
  end
end

the strong parameter private function:

def filter_page_params
  params.require(:page).permit(:parent_id, :name, :grid_layout_id)
end

There's a preload hook on update as well having @page already loaded from DB

Does anybody know what causes angular to break the request?

I apreciate any help. kind regards, Alex

¿Fue útil?

Solución

When you use PATCH request in angular-resource, the header's Content-Type will be set to 'application/xml', I assume that your backend only respond to json, and that is why you are getting the error. You will need to manually set it to 'application/json'.

One way to do this in the stable version is to use $httpProvider to set the default header

$httpProvider.defaults.headers.common["Content-Type"] = 'application/json'

In unstable version 1.1.1+ you can set it directly in $resource, I have not tried this but it is mentioned here: https://groups.google.com/forum/#!msg/angular/33kV8fjFcME/0f0y2mL2DBgJ

$resource '/users/:id',
  { id: '@id' }
  update:
    method: 'PATCH'
    headers: { 'Content-Type': 'application/json' }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top