开发者

HTTP POST rails 3 - json - 406 & 500 error

开发者 https://www.devze.com 2023-03-29 02:12 出处:网络
I am having trouble sending data through HTTP REST. When I try and POST json data through the chrome extension \'Simple REST Client\' I get a 500 internal server error stating \"The document does not

I am having trouble sending data through HTTP REST. When I try and POST json data through the chrome extension 'Simple REST Client' I get a 500 internal server error stating "The document does not have a valid root". When I curl I get a 40开发者_如何学运维6 error. But when I leave the data filed empty in both the client and curl a blank document is created! I have changed the json text to every possible valid combination and putting the data back in that the GET produces but no luck!

My controller code is:

def create
@verify = Verify.new(params[:verify])

respond_to do |format|
  if @verify.save
    format.html { redirect_to( :back, :notice => 'Verified') }
    format.json { render :json => @verify, :status => :created, :location => @verify }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @verify.errors, :status => :unprocessable_entity }
    format.json  { render :json => @verify.errors, :status => :unprocessable_entity }
  end
end

end

Sample json

{"www" : "wwww"}

I'm using Mongoid and Devise gems (tried removing before_filter from controller but that made no difference).


Rails 3 adds a root element to json by default in activerecord, since you're using mongoid, try adding this to your mongoid.yml

include_root_in_json: true
0

精彩评论

暂无评论...
验证码 换一张
取 消