访问 POST 参数 [英] accessing POST parameters

查看:36
本文介绍了访问 POST 参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 scaffold create rails 应用程序添加新产品"时,以下行正确添加了新产品

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])

当我尝试使用以下 URL 添加新产品时(尝试从 Java 程序 POST 数据).

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

更让我困惑的是,如果我使用 rails 应用程序添加新产品,params[:serial]params[:value] 变量为空.

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.

谢谢

推荐答案

Model.new 方法需要一个哈希值.

The Model.new method takes a hash.

params[:product] 实际上包含类似 {:serial =>555,:值=>111}

您要使用的网址是:

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

(确保您确实在使用 POST)

(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

祝你好运!

这篇关于访问 POST 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆