Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing POST parameters

When I add a new "product" using my scaffold create rails app, the following line properly adds a new product

@product = Product.new(params[:product])

When I try to add a new product using the following URL (trying to POST data up from a java program).

http://localhost:3000/products?serial=555&value=111

The product is not created, however I can access the "serial" and "value" values like this:

 @product = Product.new
 @product.serial=params[:serial]
 @product.value=params[:value]
 @product.save

To further confuse me, if I use the rails app to add a new product, the params[:serial] and params[:value] variables are empty.

Can someone please point me in the right direction.

Thanks

like image 233
brodie31k Avatar asked Apr 02 '09 02:04

brodie31k


1 Answers

The Model.new method takes a hash.

params[:product] actually contains something like {:serial => 555, :value => 111}

The url you would want to use is:

http://localhost:3000/products?product[serial]=555&product[value]=111

(Make sure that you are indeed using POST)

If you want to keep your current url scheme you would have to use:

@product = Product.new({:serial => params[:serial], :value => params[:value]})

You can also determine exactly what is available inside of params by printing it out to console using:

p params

Good luck!

like image 122
Gdeglin Avatar answered Oct 20 '22 22:10

Gdeglin