Creating an Action API with Rails. Do I need to use response_with

I am trying to figure out various ways to create a create action in the Rails API. Here is what I have for my index action (which works) and my current implementation of my create action.

routes.rb file:

Rails.application.routes.draw do namespace :api do namespace :v1 do resources :vendors end end end 

controller:

 class Api::V1::SuyasController < ApplicationController def index render json: Suya.all end def create render json: Suya.create(suyas_params) end private def suyas_params require(:suya).permit(:meat, :spicy) end end 

Do I need to use response_with / reply_to? This is abstracted by .gem respondents. If I do not want to use the stone of respondents, is this the best way to create api?

+1
source share
1 answer

As an API controller that is only responsible for API calls, yes, you should use the respond_to and respond_with helper methods, as shown below:

 class Api::V1::SuyasController < ApplicationController respond_to :json ... def create respond_with(Suya.create(suyas_params)) end ... end 
+1
source

All Articles