我在Heroku的服务器上创建了简单的ROR应用程序,我想使用RUBY的脚本添加产品:
要求'rubygems' 要求'rest_client'
RestClient.post'http://falling-ice-5948.herokuapp.com/products/new', :title => 'TESTTESTTEST',:description => “MYTESTTESTTESTTEST” :image_url => “TESTTESTNULL.jpg”,:price => 4.50
有我的页面:
当我运行我的脚本时,它给了我一个错误:
ruby postEasy.rb /usr/lib/ruby/gems/1.8/gems/rest-client-1.6.7/lib/restclient/abstract_response.rb:48:in 来自
return!': 404 Resource Not Found (RestClient::ResourceNotFound) from /usr/lib/ruby/gems/1.8/gems/rest-client-1.6.7/lib/restclient/request.rb:230:in
process_result' /usr/lib/ruby/gems/1.8/gems/rest-client-1.6.7/lib/restclient/request.rb:178:intransmit' from /usr/lib/ruby/1.8/net/http.rb:543:in
从'开始' /usr/lib/ruby/gems/1.8/gems/rest-client-1.6.7/lib/restclient/request.rb:172:intransmit' from /usr/lib/ruby/gems/1.8/gems/rest-client-1.6.7/lib/restclient/request.rb:64:in
执行'来自 /usr/lib/ruby/gems/1.8/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in 来自postEasy.rb的execute' from /usr/lib/ruby/gems/1.8/gems/rest-client-1.6.7/lib/restclient.rb:72:in
帖子:4
有什么想法吗?
提前致谢
答案 0 :(得分:1)
我不知道您应用的详细信息,但我希望您在http://falling-ice-5948.herokuapp.com/products/而不是http://falling-ice-5948.herokuapp.com/products/new上拨打POST请求。
GET http://falling-ice-5948.herokuapp.com/products/new将检索表单以创建新记录。
答案 1 :(得分:1)
检查rake routes
的输出。这将显示您已设置的路线和可用方法的方法。我怀疑/ products / new将显示为GET而/产品作为POST是你真正想要做的。
答案 2 :(得分:1)
您需要在RestClient.post
方法中嵌套参数。您最有可能在行动中寻找params[:product]
,但不会有任何数据。我在你的heroku应用程序上进行了测试,对不起,但这样可行(就像它对我而言):
RestClient.post 'http://falling-ice-5948.herokuapp.com/products',
:product => {
:title => 'foobarbazfoobarbaz',
:description => "foobarbazfoobarbaz description",
:image_url => "foobarbazfoobarbaz.jpg",
:price => 42.00
}