ruhungry
ruhungry

Reputation: 4706

How can I use POST method in ROR app on Heroku server?

I've created simple ROR app on Heroku's server and I want to add product using RUBY's script:

require 'rubygems' require 'rest_client'

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

There's my page:

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

When I run my script it gives me an error:

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' from /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' from /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' from /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' from postEasy.rb:4

Any ideas?

Thanks in advance

Upvotes: 0

Views: 678

Answers (3)

tbuehlmann
tbuehlmann

Reputation: 9110

You will need to nest the params in the RestClient.post method. You're most likely looking for params[:product] in your action, but there won't be any data for that. I tested on your heroku app, sorry for that, but this will work (as it does for me):

RestClient.post 'http://falling-ice-5948.herokuapp.com/products',
  :product => {
    :title => 'foobarbazfoobarbaz',
    :description => "foobarbazfoobarbaz description",
    :image_url => "foobarbazfoobarbaz.jpg",
    :price => 42.00
  }

Upvotes: 1

John Beynon
John Beynon

Reputation: 37507

Check the output of rake routes. That will show the methods for the routes you have setup and the methods available. I suspect the /products/new will show as a GET whilst /products as a POST is what you actually want be doing.

BTW, this would have occurred locally too so it's not anything to do with Heroku.

Upvotes: 1

Nick
Nick

Reputation: 2468

I don't know the details of your app but I would expect you to call the POST request on http://falling-ice-5948.herokuapp.com/products/ not http://falling-ice-5948.herokuapp.com/products/new.

GET http://falling-ice-5948.herokuapp.com/products/new would retrieve a form to create a new record.

Upvotes: 1

Related Questions