Q: Set http status code 201 to an API response in action controller rails I'm new to rails and I'm building a Rails app that will function as an API. Currently I don't have any models or database just an Api::ProductController controller:
class Api::ProductController < ApplicationController

  def create
    Rails.logger.info "product was created and the parameters are #{product_params[:name]}, #{product_params[:age]}"
  end

  private

  def product_params
    params.permit(:name, :age)
  end
end


. As I continue and wrote the request Rspec:

RSpec.describe Api::productsController, type: :request do

  it "creates a product" do
    post "/api/products", params: { name: "name", age: "22"}

    expect(response).to have_http_status(:created)
    expect(response.body).to include("product was successfully created.")
  end
end

But when I run the request rspec test on the command line I get the following error:
Failure/Error: expect(response).to have_http_status(:created)
       expected the response to have status code :created (201) but it was :no_content (204)


My question is how can I set the status code to :created (201)? is the Head method a good approach? any solution or guiding would be appreciated!

A: It is common to return json presentation of created item.
Now there is no returning data from your action.
Try to use render json: product_params.to_json, status: :created

A:   def create
    Rails.logger.info "product was created and the parameters are #{product_params[:name]}, #{product_params[:age]}"
    render status: :created
  end

