Trying to get a POST to return 400 bad request

kjs3

I have a create method that builds a new model through an association and I was expecting it to return a 400 response with some text if no params were in the POST request. However, I get an error.

This is in Rails 4.0.2

controller methods:

  def create
    @cast_profile = current_user.build_cast_profile(cast_profile_params)
    if @cast_profile.save
      redirect_to cast_profile_path
    else
      render :edit
    end
  end

  def cast_profile_params
    params.require(:cast_profile).permit(:name, :email, :public)
  end

If I pass the params its all fine but I'm trying to test the bad request scenario. Here's the error:

ActionController::ParameterMissing: param not found: cast_profile

I could rescue it explicitly but I thought strong parameters was supposed to do that automatically.

Bart

The behaviour is as follows:

Handling of Unpermitted Keys

By default parameter keys that are not explicitly permitted will be logged in the development and test environment. In other environments these parameters will simply be filtered out and ignored.

Additionally, this behaviour can be changed by changing the config.action_controller.action_on_unpermitted_parameters property in your environment files. If set to :log the unpermitted attributes will be logged, if set to :raise an exception will be raised.

(source)

I would suggest rescuing from this exception with 400 status (Bad Request):

rescue_from ActionController::ParameterMissing do
  render :nothing => true, :status => :bad_request
end

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Trying to send post and always get bad request

From Dev

POST 400 bad request in angularjs

From Dev

JSON ajax post data - get 400 bad request message

From Dev

Trying to send a POST request and get json data but getting 400

From Dev

Bad Request (400) on $.ajax post request flask

From Dev

POST request with python 400 Bad Request

From Dev

HTTP request failed! HTTP/1.0 400 Bad Request when trying to get content from youtube

From Dev

400 Bad Request sending JSON POST to REST

From Dev

'400 Bad Request' when post json in Flask

From Dev

Python Post call throwing 400 Bad Request

From Dev

Spring RestController POST 400 Bad Request

From Dev

POST method is ajax gives 400 (Bad Request)

From Dev

400 Bad Request Error in AJAX POST

From Dev

MEAN : Getting Error 400 (Bad Request) on POST

From Dev

Afnetworking - post json -> 400 bad Request

From Dev

WCF Post Method, returns 400 Bad Request

From Dev

HttpURLConnection POST returning 400 Bad Request

From Dev

400 Bad Request for $http post method

From Dev

C++ Http POST 400 bad request

From Dev

Python POST request 400 Bad requst

From Dev

400 bad request Ajax post via CLI

From Dev

Return bad request (400) for missing required parameter

From Dev

AutoValidateAntiforgeryToken always causing 400 Bad Request to return

From Dev

400 Bad Request when sending http post request to get token from auth code?

From Dev

400 Bad Request when sending http post request to get token from auth code?

From Dev

iOS AFNetworking post request returns Request failed: bad request (400)

From Dev

Why Im get 400 bad request?

From Dev

How get 400 Bad Request in variable

From Dev

Unexpected Error 400:Bad Request HttpBuilder POST Request

Related Related

HotTag

Archive