Goutham Palani Goutham Palani - 4 months ago 11
JSON Question

When JSON data posted in rails throws param is missing or the value is empty: stall

I have been trying post JSON data via POSTMAN but I get param is missing or the value is empty: stall. can someone please tell me where I'm going wrong ?

def create
@stall = Stall.new(stall_params)

if @stall.save
render json: @stall, status: :created
else
render json: @stall.errors, status: :unprocessable_entity
end
end



private
def stall_params
params.require(:stall).permit(:name, :place)
end


My Table contains only two columns name and place

Below is the log
Processing by API::V1::StallsController#create as JSON
Parameters: {"name"=>"trill", "place"=>"trill"}
Completed 400 Bad Request in 0ms (ActiveRecord: 0.0ms)

ActionController::ParameterMissing (param is missing or the value is empty: stall):
app/controllers/api/v1/stalls_controller.rb:47:in
stall_params'
app/controllers/api/v1/stalls_controller.rb:27:in
create'

Rendered /home/goutham/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_source.erb (7.4ms)
Rendered /home/goutham/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (16.0ms)
Rendered /home/goutham/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.9ms)
Rendered /home/goutham/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (135.0ms)

Answer

Your stall_params method searches for params in the 'stall' namespace.

Your JSON parameters must have to following format:

'stall': {
  'name': 'trill',
  'place': 'trill'
}

If you want the parameters in the top namespace, you have to remove the #require call in stall_params.

params.permit(:name, :place)