当在rails中发布的JSON数据抛出param时,或者值为空:停止

时间:2016-08-01 14:42:58

标签: ruby-on-rails json activerecord

我一直在尝试通过POSTMAN发布JSON数据但是我得到了#34; param丢失或者值为空:停止"。有人可以告诉我哪里出错了?

 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

我的表只包含两列名称和地点

以下是日志

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)

1 个答案:

答案 0 :(得分:3)

您的stall_params方法会在'stall'命名空间中搜索params。

您的JSON参数必须采用以下格式:

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

如果您想要顶部命名空间中的参数,则必须删除#require中的stall_params来电。

params.permit(:name, :place)