Jared
Jared

Reputation: 588

Ruby on Rails - Post request being rejected when testing out REST api?

I am pretty new to ruby on rails and completely new to designing and implementing REST apis. I have one running and am trying to make a simple post request to save something in my database via curl.

Here are my routes:

GET  /api/v1/employees(.:format)                           api_employees#index
   POST /api/v1/employees(.:format)                           api_employees#create
   GET  /api/v1/employees/:id(.:format)                       api_employees#show
   PUT  /api/v1/employees/:id(.:format)                       api_employees#update
   GET  /api/v1/employees/:id/addresses(.:format)             api_addresses#index
   POST /api/v1/employees/:id/addresses(.:format)             api_addresses#create
   GET  /api/v1/employees/:id/addresses/:address_id(.:format) api_addresses#show
   PUT  /api/v1/employees/:id/addresses/:address_id(.:format) api_addresses#update

and here is my api_employees controller. I haven't made the addresses controller yet but I am trying to post an employee.

class ApiEmployeesController < BaseApiController
before_filter :find_employee, only: [:show, :update]

before_filter only: :create do
  unless @json.has_key?('employee') && @json['employee']['address']
    render nothing: true, status: :bad_request
  end
end

before_filter only: :update do
  unless @json.has_key?('employee')
    render nothing: true, status: :bad_request
  end
end

before_filter only: :create do
  @employee = Employee.find_by_id(@json['employee']['id'])
end

def index
  render json: Employee.all
end

def show
  render json: @employee
end

def create
  if @employee.present?
    render nothing: true, status: :conflict
  else
    @employee = Employee.new
    @employee.assign_attributes(@json['employee'])
    if @employee.save
      render json: @employee
    else
      render nothing: true, status: :bad_request
    end
  end
end

def update
  @employee.assign_attributes(@json['employee'])
  if @employee.save
    render json: @employee
  else
    render nothing: true, status: :bad_request
  end
end

 private
   def find_employee
     @employee = Employee.find_by_id(params[:id])
     render nothing: true, status: :not_found unless @employee.present?
   end
end

I am trying to post using: curl -H "Content-Type: application/json" -X POST -d '{"employee":{"address":"123.123.123.123"}}' http://myapi.heroku.com/api/v1/employees

and I get the response

<body>
<!-- This file lives in public/422.html -->
<div class="dialog">
  <div>
    <h1>The change you wanted was rejected.</h1>
    <p>Maybe you tried to change something you didn't have access to.</p>
  </div>
  <p>If you are the application owner check the logs for more information.</p>
</div>

Do I need to change the access somehow? Any help is much appreciated.

Upvotes: 0

Views: 453

Answers (1)

Jared
Jared

Reputation: 588

Just needed skip_before_action :verify_authenticity_token

Upvotes: 2

Related Questions