Skip to content
This repository was archived by the owner on Aug 17, 2017. It is now read-only.
This repository was archived by the owner on Aug 17, 2017. It is now read-only.

Issues with errors in json context #157

@djcp

Description

@djcp

If you're using strong_parameters for json requests and get an attribute failure on a root attribute, it appears that strong_parameters does not return a json-encoded error message.

  def create
    @notice = Notice.new(notice_params)

    respond_to do |format|
      if @notice.save
        format.html { redirect_to :root, notice: "Notice created!" }
        format.json { head :created, location: @notice }
      else
        format.html { render :new }
        format.json do
          render json: @notice.errors, status: :unprocessable_entity
        end
      end
    end
  end

  def notice_params
     params.require(:notice).permit(:title)
  end

With a strong parameter failure:

$ curl -H "Accept: application/json" -H "Content-type: application/json" 'http://localhost:5000/notices/' -d '{ "notnotice": { "title": "foobar"} }'

Required parameter missing: notice%
$

With a model-level validation failure:

$ curl -H "Accept: application/json" -H "Content-type: application/json" 'http://localhost:5000/notices/' -d '{ "notice": { "bleep": "foo"} }' 

 {"title":["can't be blank"]}
$

I'm not entirely sure of a fix here, perhaps a default json response for these types of errors?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions