【问题标题】:How can I use POST method in ROR app on Heroku server?如何在 Heroku 服务器上的 ROR 应用程序中使用 POST 方法?
【发布时间】:2011-12-20 07:26:59
【问题描述】:

我在 Heroku 的服务器上创建了简单的 ROR 应用程序,我想使用 RUBY 的脚本添加产品

需要“红宝石” 需要'rest_client'

RestClient.post 'http://falling-ice-5948.herokuapp.com/products/new', :title => 'TESTTESTTEST', :description => "MYTESTTESTTESTTEST", :image_url => "TESTTESTNULL.jpg", :price => 4.50

这是我的页面:

http://falling-ice-5948.herokuapp.com/products/new

当我运行我的脚本时,它给了我一个错误:

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:in transmit' from /usr/lib/ruby/1.8/net/http.rb:543:instart' 来自 /usr/lib/ruby/gems/1.8/gems/rest-client-1.6.7/lib/restclient/request.rb:172:in transmit' from /usr/lib/ruby/gems/1.8/gems/rest-client-1.6.7/lib/restclient/request.rb:64:in execute' 来自 /usr/lib/ruby/gems/1.8/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in execute' from /usr/lib/ruby/gems/1.8/gems/rest-client-1.6.7/lib/restclient.rb:72:in post' 来自 postEasy.rb:4

有什么想法吗?

提前致谢

【问题讨论】:

  • 您只是想创建一个产品吗?如果是这样,您也可以在 heroku 上的 rails 控制台中使用 $ heroku console 进行操作。
  • 我想创建一个自动执行的 ruby​​ 脚本。你不明白吗?
  • 所以你实际上只是想在你的数据库中植入记录,对吧?
  • 几乎...Fe:我会给你这个脚本,你可以用它来添加你的产品。也许重要的是在 Heroku 的服务器上......
  • 你为什么要这么做?是否有充分的理由通过某种 HTTP API 添加产品?

标签: ruby-on-rails ruby post heroku


【解决方案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
  }

【讨论】:

  • 嗯,我的帖子必须有一个“检查”按钮。
【解决方案2】:

检查rake routes 的输出。这将显示您设置的路线的方法和可用的方法。我怀疑 /products/new 将显示为 GET,而 /products 作为 POST 是您真正想要做的。

顺便说一句,这也会发生在本地,因此与 Heroku 无关。

【讨论】:

  • 产品 GET /products(.:format) {:action=>"index", :controller=>"products"} POST /products(.:format) {:action=>"create" , :controller=>"products"} new_product GET /products/new(.:format) {:action=>"new", :controller=>"products"} edit_product GET /products/:id/edit(.:format ) {:action=>"edit", :controller=>"products"} product GET /products/:id(.:format) {:action=>"show", :controller=>"products"} PUT /products /:id(.:format) {:action=>"update", :controller=>"products"} 我应该使用哪个以及如何使用?
  • POST /products(.:format) {:action=>"create",:controller=>"products"} 是关键。将您的代码更改为; RestClient.post 'falling-ice-5948.herokuapp.com/products', :title => 'TESTTESTTEST', :description => "MYTESTTESTTESTTEST", :image_url => "TESTTESTNULL.jpg", :price => 4.50
【解决方案3】:

我不知道您的应用程序的详细信息,但我希望您在 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 将检索表单以创建新记录。

【讨论】:

  • 我尝试这样做:RestClient.post 'falling-ice-5948.herokuapp.com/products', {:action=>"create", :controller=>"products", :title => ' TESTTESTTEST', :description => "MYTESTTESTTESTTEST", :image_url => "TESTTESTNULL.jpg", :price => 4.50 } 但还是不行
  • 您是否尝试过与您最初发布的示例完全相同但只是从 url 末尾删除新的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-30
  • 2015-11-30
  • 2020-01-23
  • 1970-01-01
  • 1970-01-01
  • 2012-01-15
  • 1970-01-01
相关资源
最近更新 更多