R
R
Roman Mirilaczvili2016-06-26 17:02:46
API
Roman Mirilaczvili, 2016-06-26 17:02:46

How to return 'Unpermitted parameter' error when validating fields in REST API?

There is code like this:

def safe_params(params)
    params.require(:survey).permit(:name, :question)
  end

  def update
    if @survey.update(safe_params(params))
      render json: { status: "Success" }
    else
      render json: { status: "Error", message: 'Validation errors', errors: @survey.errors }
    end
  end

in which, if an invalid parameter name is specified, the error is silently ignored and status: "Success" is displayed.
How can I specify that when checking safe_paramsin @survey.errors this type of error is handled?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Mirilaczvili, 2016-06-27
@2ord

Can be defined in a file config/environments/development.rbfor the entire project:
You also need to decide how to proceed for production in the config/environments/production.rb.
If this behavior is only needed for the API controller, then

class ApiController < ApplicationController

# <<<< добавить
  # Always raise errors on unpermitted parameters
  ActionController::Parameters.action_on_unpermitted_parameters = :raise
# <<<<

end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question