I am going to create a soothing API using Ruby on Rails. I want to create, delete, show and update data. All of them must be JSON in order to get it on Android devices. I also use Postman to test my APIs. This is what I did:
My controller:
class Api::V1::UsersController < ApplicationController respond_to :json def show respond_with User.find(params[:id]) end def create user=User.new(user_params) if user.save render json: user, status: 201 else render json: {errors: user.errors}, status: 422 end end def update user=User.find(params[:id]) if user.update(user_params) render json: user, status:200 else render json: {erros: user.errors},status: 422 end end def destroy user=User.find(params[:id]) user.destroy head 204 end private def user_params params.require(:user).permit(:email,:password,:password_confirmation) end end
and this is my route file:
Rails.application.routes.draw do devise_for :users namespace :api, defaults:{ format: :json } do namespace :v1 do resources :users, :only=>[:show,:create,:update,:destroy] end end end
and also added the following code to my Gemfile:
gem "devise" gem 'active_model_serializers'
I do not know why, when I want to create through the postman, I get the following error:
ActionController InvalidAuthenticityToken in Api::V1::UsersController
source share